鍍金池/ 問答/人工智能  數(shù)據(jù)分析&挖掘  Python/ scrapy 爬蟲關(guān)閉但實際任務(wù)并沒完成

scrapy 爬蟲關(guān)閉但實際任務(wù)并沒完成

2017-10-17 11:48:18 [scrapy.core.engine] INFO: Closing spider (finished)
29957 2017-10-17 11:48:19 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
29958 {'downloader/request_bytes': 2175351,
29959  'downloader/request_count': 6472,
29960  'downloader/request_method_count/GET': 6472,
29961  'downloader/response_bytes': 494012842,
29962  'downloader/response_count': 6472,
29963  'downloader/response_status_count/200': 6419,
29964  'downloader/response_status_count/301': 53,
29965  'dupefilter/filtered': 8,
29966  'finish_reason': 'finished',
29967  'finish_time': datetime.datetime(2017, 10, 17, 11, 48, 19, 59301),
29968  'item_scraped_count': 6417,
29969  'log_count/DEBUG': 12891,
29970  'log_count/ERROR': 1,
29971  'log_count/INFO': 120,
29972  'memusage/max': 385605632,
29973  'memusage/startup': 94334976,
29974  'request_depth_max': 1,
29975  'response_received_count': 6419,
29976  'scheduler/dequeued': 6471,
29977  'scheduler/dequeued/memory': 6471,
29978  'scheduler/enqueued': 6471,
29979  'scheduler/enqueued/memory': 6471,
29980  'spider_exceptions/RuntimeError': 1,
29981  'start_time': datetime.datetime(2017, 10, 17, 9, 55, 15, 21744)}
29982 2017-10-17 11:48:19 [scrapy.core.engine] INFO: Spider closed (finished)

可以看到,我的爬蟲其實只爬了6472個,但是實際上讀進來的文件里有228336個job要爬,但爬蟲卻說finish reason:“finished"
貼上代碼:

def parse(self, response):
          print("number of the id_dict is %s"%len(self.house_id_dict))
          for id in self.house_id_dict.keys():    #house ID eg.44225621 
              yield response.follow("/for-sale/details/"+id, callback=self.parse_house)
          print("yield all the links!")

這是一個房地產(chǎn)網(wǎng)站爬蟲,我之前抓了20萬左右房子的ID號碼(記錄在了文件里), 然后讀取這些ID號(house_id_dict)再次驗證這些房子是否更新了信息。也就是說,我讓爬蟲爬取指定的url請求(url構(gòu)成是 www.xxx.com/for-sale/details/id號 eg.12345678)
照理來說沒錯啊,我查了半天,程序也沒內(nèi)存泄漏,不是系統(tǒng)kill的,scrapy為何自己結(jié)束了呢?
結(jié)束的時候也沒執(zhí)行這行代碼,是不是因為隊列里任務(wù)太多,直接爆了?

print("yield all the links!")
回答
編輯回答
鹿惑

你可以將這個spider類的代碼全部粘貼出來,更利于回答者發(fā)現(xiàn)問題。

從scrapy的統(tǒng)計日志中可以看出scrapy是正常結(jié)束爬取,不是因為隊列爆了。所以這里最可能的問題是 self.house_id_dict這個字典中 確實只有6472個key,而你說的20w的key,可能去重后是6472(猜的)

print("number of the id_dict is %s"%len(self.house_id_dict))

另外,這條語句輸出多少呢?

2017年1月27日 11:42
編輯回答
小曖昧

我也是遇到了同樣的問題 ,請問是怎么解決的嗎?明明是一直循環(huán)的, 加了很多異常檢測, 但是還是任務(wù)完成,查看日志只有
[<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion: Connection lost.>]

2018年2月19日 16:18
編輯回答
不討囍

有可能是讀取進來的ID存在不規(guī)范。

例如第6473個ID不規(guī)范,則scrapy在此時報錯,而繼續(xù)爬取之前的網(wǎng)頁。這樣最后scrapy就算正常finished

2018年3月20日 11:39