python - scrapy url去重
問題描述
請問scrapy是url自動去重的嗎?比如下面這段代碼,為什么運行時start_urls里面的重復url會重復爬取了?
class TestSpider(scrapy.Spider): name = 'test' allowed_domains = ['baidu.com'] start_urls = [’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’, ’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’, ’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’,] def parse(self, response):for sel in response.xpath(’//p[@class='grid-list grid-list-spot']/ul/li’): item = TestspiderItem() item[’title’] = sel.xpath(’p[@class='list']/a/text()’)[0].extract() item[’link’] = sel.xpath(’p[@class='list']/a/@href’)[0].extract() yield item
問題解答
回答1:建一個Url管理器,就不會重復抓取了
回答2:知道了,改成這樣就可以了。
def start_requests(self):
yield scrapy.Request(’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’, self.parse)yield scrapy.Request(’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’, self.parse)yield scrapy.Request(’http://baike.baidu.com/fenlei/%E5%A8%B1%E4%B9%90%E4%BA%BA%E7%89%A9’, self.parse)
相關文章:
1. MySQL數據庫中文亂碼的原因2. angular.js - 關于$apply()3. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下4. angular.js使用$resource服務把數據存入mongodb的問題。5. css - C#與java開發Windows程序哪個好?6. dockerfile - [docker build image失敗- npm install]7. nignx - docker內nginx 80端口被占用8. mysql - 新浪微博中的關注功能是如何設計表結構的?9. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???10. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?
