解決Scrapy-Redis爬取完畢以後繼續空跑的問題
1. 背景
根據scrapy-redis分佈式爬蟲的原理,多臺爬蟲主機共享一個爬取隊列。當爬取隊列中存在request時,爬蟲就會取出request進行爬取,若是爬取隊列中不存在request時,爬蟲就會處於等待狀態,行以下:html
-
E:\Miniconda\python.exe E:/PyCharmCode/redisClawerSlaver/redisClawerSlaver/spiders/main.py
-
2017 -12 -12 15: 54: 18 [scrapy.utils.log] INFO: Scrapy 1.4 .0 started (bot: scrapybot)
-
2017 -12 -12 15: 54: 18 [scrapy.utils.log] INFO: Overridden settings: { 'SPIDER_LOADER_WARN_ONLY': True}
-
2017 -12 -12 15: 54: 18 [scrapy.middleware] INFO: Enabled extensions:
-
[ 'scrapy.extensions.corestats.CoreStats',
-
'scrapy.extensions.telnet.TelnetConsole',
-
'scrapy.extensions.logstats.LogStats']
-
2017 -12 -12 15: 54: 18 [myspider_redis] INFO: Reading start URLs from redis key 'myspider:start_urls' (batch size: 110, encoding: utf -8
-
2017 -12 -12 15: 54: 18 [scrapy.middleware] INFO: Enabled downloader middlewares:
-
[ 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
-
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
-
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
-
'redisClawerSlaver.middlewares.ProxiesMiddleware',
-
'redisClawerSlaver.middlewares.HeadersMiddleware',
-
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
-
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
-
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
-
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
-
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
-
'scrapy.downloadermiddlewares.stats.DownloaderStats']
-
2017 -12 -12 15: 54: 18 [scrapy.middleware] INFO: Enabled spider middlewares:
-
[ 'scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
-
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
-
'scrapy.spidermiddlewares.referer.RefererMiddleware',
-
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
-
'scrapy.spidermiddlewares.depth.DepthMiddleware']
-
2017 -12 -12 15: 54: 18 [scrapy.middleware] INFO: Enabled item pipelines:
-
[ 'redisClawerSlaver.pipelines.ExamplePipeline',
-
'scrapy_redis.pipelines.RedisPipeline']
-
2017 -12 -12 15: 54: 18 [scrapy.core.engine] INFO: Spider opened
-
2017 -12 -12 15: 54: 18 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
-
2017 -12 -12 15: 55: 18 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
-
2017 -12 -12 15: 56: 18 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
- 但是,若是全部的request都已經爬取完畢了呢?這件事爬蟲程序是不知道的,它沒法區分結束和空窗期狀態的不一樣,因此會一直處於上面的那種等待狀態,也就是咱們說的空跑。
- 那有沒有辦法讓爬蟲區分這種狀況,自動結束呢?
2. 環境
- 系統:win7
- scrapy-redis
- redis 3.0.5
- python 3.6.1
3. 解決方案
- 從背景介紹來看,基於scrapy-redis分佈式爬蟲的原理,爬蟲結束是一個很模糊的概念,在爬蟲爬取過程當中,爬取隊列是一個不斷動態變化的過程,隨着request的爬取,又會有新的request進入爬取隊列。進進出出。爬取速度高於填充速度,就會有隊列空窗期(爬取隊列中,某一段時間會出現沒有request的狀況),爬取速度低於填充速度,就不會出現空窗期。因此對於爬蟲結束這件事來講,只能模糊定義,沒有一個精確的標準。
- 因此,下面這兩種方案都是一種大概的思路。
3.1. 利用scrapy的關閉spider擴展功能
-
-
class scrapy.contrib.closespider.CloseSpider
-
當某些情況發生,spider會自動關閉。每種狀況使用指定的關閉緣由。
-
-
關閉spider的狀況能夠經過下面的設置項配置:
-
-
CLOSESPIDER_TIMEOUT
-
CLOSESPIDER_ITEMCOUNT
-
CLOSESPIDER_PAGECOUNT
-
CLOSESPIDER_ERRORCOUNT
- CLOSESPIDER_TIMEOUT
-
CLOSESPIDER_TIMEOUT
-
默認值: 0
-
-
一個整數值,單位爲秒。若是一個spider在指定的秒數後仍在運行, 它將以 closespider_timeout 的緣由被自動關閉。 若是值設置爲 0(或者沒有設置),spiders不會由於超時而關閉。
- CLOSESPIDER_ITEMCOUNT
-
CLOSESPIDER_ITEMCOUNT
-
缺省值: 0
-
-
一個整數值,指定條目的個數。若是spider爬取條目數超過了指定的數, 而且這些條目經過item pipeline傳遞,spider將會以 closespider_itemcount 的緣由被自動關閉。
- CLOSESPIDER_PAGECOUNT
-
CLOSESPIDER_PAGECOUNT
-
0 .11 新版功能.
-
-
缺省值: 0
-
-
一個整數值,指定最大的抓取響應( reponses)數。 若是 spider抓取數超過指定的值,則會以 closespider_pagecount 的緣由自動關閉。 若是設置爲0(或者未設置), spiders不會由於抓取的響應數而關閉。
- CLOSESPIDER_ERRORCOUNT
-
CLOSESPIDER_ERRORCOUNT
-
0 .11 新版功能.
-
-
缺省值: 0
-
-
一個整數值,指定 spider能夠接受的最大錯誤數。 若是 spider生成多於該數目的錯誤,它將以 closespider_errorcount 的緣由關閉。 若是設置爲0(或者未設置), spiders不會由於發生錯誤過多而關閉。
- 示例:打開 settings.py,添加一個配置項,以下
-
-
CLOSESPIDER_TIMEOUT = 84600
- 特別注意:若是爬蟲在規定時限沒有把request所有爬取完畢,此時強行中止的話,爬取隊列中就還會存有部分request請求。那麼爬蟲下次開始爬取時,必定要記得在master端對爬取隊列進行清空操做。
3.2. 修改scrapy-redis源碼
-
# ----------- 修改scrapy-redis源碼時,特別須要注意的是:---------
-
# 第一,要留有原始代碼的備份。
-
# 第二,當項目移植到其餘機器上時,須要將scrapy-redis源碼一塊兒移植過去。通常代碼位置在\Lib\site-packages\scrapy_redis\下
- 想象一下,爬蟲已經結束的特徵是什麼?那就是爬取隊列已空,從爬取隊列中沒法取到request信息。那着手點應該就在從爬取隊列中獲取request和調度這個部分。查看scrapy-redis源碼,咱們發現了兩個着手點:
3.2.1. 細節python
-
# .\Lib\site-packages\scrapy_redis\schedluer.py
-
def next_request(self):
-
block_pop_timeout = self.idle_before_close
-
# 下面是從爬取隊列中彈出request
-
# 這個block_pop_timeout 我還沒有研究清除其做用。不過確定不是超時時間......
-
request = self.queue.pop(block_pop_timeout)
-
if request and self. stats:
-
self.stats.inc_value( 'scheduler/dequeued/redis', spider= self.spider)
-
return request
- 9
-
# .\Lib\site-packages\scrapy_redis\spiders.py
-
def next_requests(self):
-
"" "Returns a request to be scheduled or none." ""
-
use_set = self.settings.getbool( 'REDIS_START_URLS_AS_SET', defaults.START_URLS_AS_SET)
-
fetch_one = self.server.spop if use_set else self.server.lpop
-
# XXX: Do we need to use a timeout here?
-
found = 0
-
# TODO: Use redis pipeline execution.
-
while found < self. redis_batch_size:
-
data = fetch_one( self.redis_key)
-
if not data:
-
# 表明爬取隊列爲空。可是多是永久爲空,也多是暫時爲空
-
# Queue empty.
-
break
-
req = self.make_request_from_data(data)
-
if req:
-
yield req
-
found += 1
-
else:
-
self.logger.debug( "Request not made from data: %r", data)
-
-
if found:
-
self.logger.debug( "Read %s requests from '%s'", found, self.redis_key)
- 參考註釋,從上述源碼來看,就只有這兩處能夠作手腳。可是爬蟲在爬取過程當中,隊列隨時均可能出現暫時的空窗期。想判斷爬取隊列爲空,通常是設定一個時限,若是在一個時段內,隊列一直持續爲空,那咱們能夠基本認定這個爬蟲已經結束了。因此有了以下的改動:
-
# .\Lib\site-packages\scrapy_redis\schedluer.py
-
-
# 原始代碼
-
def next_request(self):
-
block_pop_timeout = self.idle_before_close
-
request = self.queue.pop(block_pop_timeout)
-
if request and self. stats:
-
self.stats.inc_value( 'scheduler/dequeued/redis', spider= self.spider)
-
return request
-
-
# 修改後的代碼
-
def __init__(self, server,
-
persist=False,
-
flush_on_start=False,
-
queue_key=defaults.SCHEDULER_QUEUE_KEY,
-
queue_cls=defaults.SCHEDULER_QUEUE_CLASS,
-
dupefilter_key=defaults.SCHEDULER_DUPEFILTER_KEY,
-
dupefilter_cls=defaults.SCHEDULER_DUPEFILTER_CLASS,
-
idle_before_close=0,
-
serializer=None):
-
# ......
-
# 增長一個計數項
-
self.lostGetRequest = 0
-
-
def next_request(self):
-
block_pop_timeout = self.idle_before_close
-
request = self.queue.pop(block_pop_timeout)
-
if request and self. stats:
-
# 若是拿到了就恢復這個值
-
self.lostGetRequest = 0
-
self.stats.inc_value( 'scheduler/dequeued/redis', spider= self.spider)
-
if request is None:
-
self.lostGetRequest += 1
-
print(f "request is None, lostGetRequest = {self.lostGetRequest}, time = {datetime.datetime.now()}")
-
# 100個大概8分鐘的樣子
-
if self.lostGetRequest > 200:
-
print(f "request is None, close spider.")
-
# 結束爬蟲
-
self.spider.crawler.engine.close_spider( self.spider, 'queue is empty')
-
return request
- 相關log信息
-
2017 -12 -14 16: 18: 06 [scrapy.middleware] INFO: Enabled item pipelines:
-
[ 'redisClawerSlaver.pipelines.beforeRedisPipeline',
-
'redisClawerSlaver.pipelines.amazonRedisPipeline',
-
'scrapy_redis.pipelines.RedisPipeline']
-
2017 -12 -14 16: 18: 06 [scrapy.core.engine] INFO: Spider opened
-
2017 -12 -14 16: 18: 06 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
-
request is None, lostGetRequest = 1, time = 2017 -12 -14 16: 18: 06.370400
-
request is None, lostGetRequest = 2, time = 2017 -12 -14 16: 18: 11.363400
-
request is None, lostGetRequest = 3, time = 2017 -12 -14 16: 18: 16.363400
-
request is None, lostGetRequest = 4, time = 2017 -12 -14 16: 18: 21.362400
-
request is None, lostGetRequest = 5, time = 2017 -12 -14 16: 18: 26.363400
-
request is None, lostGetRequest = 6, time = 2017 -12 -14 16: 18: 31.362400
-
request is None, lostGetRequest = 7, time = 2017 -12 -14 16: 18: 36.363400
-
request is None, lostGetRequest = 8, time = 2017 -12 -14 16: 18: 41.362400
-
request is None, lostGetRequest = 9, time = 2017 -12 -14 16: 18: 46.363400
-
request is None, lostGetRequest = 10, time = 2017 -12 -14 16: 18: 51.362400
-
2017 -12 -14 16: 18: 56 [scrapy.core.engine] INFO: Closing spider (queue is empty)
-
request is None, lostGetRequest = 11, time = 2017 -12 -14 16: 18: 56.363400
-
request is None, close spider.
-
登陸結果:loginRes = ( 235, b 'Authentication successful')
-
登陸成功,code = 235
-
mail has been send successfully. message:Content-Type: text/plain; charset= "utf-8"
-
MIME-Version: 1.0
-
Content-Transfer-Encoding: base64
-
From: 548516910@qq.com
-
To: 548516910@qq.com
-
Subject: =?utf -8?b? 54is6Jmr57uT5p2f54q25oCB5rGH5oql77yabmFtZSA9IHJlZGlzQ2xhd2VyU2xhdmVyLCByZWFzb24gPSBxdWV1ZSBpcyBlbXB0eSwgZmluaXNoZWRUaW1lID0gMjAxNy0xMi0xNCAxNjoxODo1Ni4zNjQ0MDA=?=
-
-
57uG6IqC77yacmVhc29uID0gcXVldWUgaXMgZW1wdHksIHN1Y2Nlc3NzISBhdDoyMDE3LTEyLTE0
-
IDE2OjE4OjU2LjM2NDQwMA==
-
-
2017 -12 -14 16: 18: 56 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
-
{ 'finish_reason': 'queue is empty',
-
'finish_time': datetime.datetime(2017, 12, 14, 8, 18, 56, 364400),
-
'log_count/INFO': 8,
-
'start_time': datetime.datetime(2017, 12, 14, 8, 18, 6, 362400)}
-
2017 -12 -14 16: 18: 56 [scrapy.core.engine] INFO: Spider closed (queue is empty)
-
Unhandled Error
-
Traceback (most recent call last):
-
File "E:\Miniconda\lib\site-packages\scrapy\commands\runspider.py", line 89, in run
-
self.crawler_process.start()
-
File "E:\Miniconda\lib\site-packages\scrapy\crawler.py", line 285, in start
-
reactor.run(installSignalHandlers= False) # blocking call
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 1243, in run
-
self.mainLoop()
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 1252, in mainLoop
-
self.runUntilCurrent()
-
--- <exception caught here> ---
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 878, in runUntilCurrent
-
call.func(* call.args, ** call.kw)
-
File "E:\Miniconda\lib\site-packages\scrapy\utils\reactor.py", line 41, in __call__
-
return self._func(*self._a, **self._kw)
-
File "E:\Miniconda\lib\site-packages\scrapy\core\engine.py", line 137, in _next_request
-
if self.spider_is_idle(spider) and slot.close_if_idle:
-
File "E:\Miniconda\lib\site-packages\scrapy\core\engine.py", line 189, in spider_is_idle
-
if self.slot.start_requests is not None:
-
builtins.AttributeError: 'NoneType' object has no attribute 'start_requests'
-
-
2017 -12 -14 16: 18: 56 [twisted] CRITICAL: Unhandled Error
-
Traceback (most recent call last):
-
File "E:\Miniconda\lib\site-packages\scrapy\commands\runspider.py", line 89, in run
-
self.crawler_process.start()
-
File "E:\Miniconda\lib\site-packages\scrapy\crawler.py", line 285, in start
-
reactor.run(installSignalHandlers= False) # blocking call
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 1243, in run
-
self.mainLoop()
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 1252, in mainLoop
-
self.runUntilCurrent()
-
--- <exception caught here> ---
-
File "E:\Miniconda\lib\site-packages\twisted\internet\base.py", line 878, in runUntilCurrent
-
call.func(* call.args, ** call.kw)
-
File "E:\Miniconda\lib\site-packages\scrapy\utils\reactor.py", line 41, in __call__
-
return self._func(*self._a, **self._kw)
-
File "E:\Miniconda\lib\site-packages\scrapy\core\engine.py", line 137, in _next_request
-
if self.spider_is_idle(spider) and slot.close_if_idle:
-
File "E:\Miniconda\lib\site-packages\scrapy\core\engine.py", line 189, in spider_is_idle
-
if self.slot.start_requests is not None:
-
builtins.AttributeError: 'NoneType' object has no attribute 'start_requests'
-
-
-
Process finished with exit code 0
- 有一個問題,如上所述,當經過engine.close_spider(spider, ‘reason’)來關閉spider時,有時會出現幾個錯誤以後才能關閉。多是由於scrapy會開啓多個線程同時抓取,而後其中一個線程關閉了spider,其餘線程就找不到spider纔會報錯。
3.2.2. 注意事項react
整個調度過程以下:redis
-
scheduler.py
scrapy -
queue.py
分佈式 - 因此,PriorityQueue和另外兩種隊列FifoQueue,LifoQueue有所不一樣,特別須要注意。若是會使用到timeout這個參數,那麼在setting中就只能指定爬取隊列爲FifoQueue或LifoQueue。
-
# 指定排序爬取地址時使用的隊列,
-
# 默認的 按優先級排序(Scrapy默認),由sorted set實現的一種非FIFO、LIFO方式。
-
# 'SCHEDULER_QUEUE_CLASS': 'scrapy_redis.queue.SpiderPriorityQueue',
-
# 可選的 按先進先出排序(FIFO)
-
'SCHEDULER_QUEUE_CLASS': 'scrapy_redis.queue.SpiderQueue',
-
# 可選的 按後進先出排序(LIFO)
-
# SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderStack'https://blog.csdn.net/mr_hui_/article/details/81455387