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. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. boot2docker無法啟動3. docker-compose中volumes的問題4. java - SSH框架中寫分頁時service層中不能注入分頁類5. docker安裝后出現Cannot connect to the Docker daemon.6. javascript - mock.js可以存儲數據嗎7. nignx - docker內nginx 80端口被占用8. docker api 開發的端口怎么獲取?9. dockerfile - 為什么docker容器啟動不了?10. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值

網公網安備