我试图收到两个请求,但只有第一个请求被调用。更具体地说,似乎只调用了第一个回调parse()。根据我的输出,我将parse2指定为第二个回调,但它似乎根本没有被调用。
import scrapy
class ExampleSpider(scrapy.Spider):
name = "example"
allowed_domains = ["https://www.reddit.com"]
start_urls = (
'https://www.reddit.com/',
)
def start_requests(self):
return [scrapy.Request(
self.start_urls[0],
method='GET'
)]
def parse(self, response):
return [scrapy.Request(self.start_urls[0], callback=self.parse2)]
def parse2(self, response):
print(response.body[:40])
输出:
2016-01-15 01:19:19 [scrapy] INFO: Scrapy 1.0.4 started (bot: example)
2016-01-15 01:19:19 [scrapy] INFO: Optional features available: ssl, http11
2016-01-15 01:19:19 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'example.spiders', 'SPIDER_MODULES': ['example.spiders'], 'BOT_NAME': 'example'}
2016-01-15 01:19:19 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2016-01-15 01:19:19 [scrapy] INFO: Enabled downloader middlewares: DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, HttpAuthMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2016-01-15 01:19:19 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2016-01-15 01:19:19 [scrapy] INFO: Enabled item pipelines:
2016-01-15 01:19:19 [scrapy] INFO: Spider opened
2016-01-15 01:19:19 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-01-15 01:19:19 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-01-15 01:19:19 [scrapy] DEBUG: Crawled (200) <GET https://www.reddit.com/> (referer: None)
2016-01-15 01:19:19 [scrapy] DEBUG: Filtered offsite request to 'www.reddit.com': <GET https://www.reddit.com/>
2016-01-15 01:19:19 [scrapy] INFO: Closing spider (finished)
2016-01-15 01:19:19 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 212,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 22307,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2016, 1, 15, 6, 19, 19, 898275),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'offsite/domains': 1,
'offsite/filtered': 1,
'request_depth_max': 1,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2016, 1, 15, 6, 19, 19, 692480)}
答案 0 :(得分:2)
那是因为它被过滤掉为重复请求。要在发出请求时更改行为,请通过dont_filter=True
:
def parse(self, response):
return scrapy.Request(self.start_urls[0],
callback=self.parse2,
dont_filter=True)