python - scrapy url去重
問題描述
請問scrapy是url自動去重的嗎?比如下面這段代碼,為什么運行時start_urls里面的重復(fù)url會重復(fù)爬取了?
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管理器,就不會重復(fù)抓取了
回答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)
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題2. javascript - 用JS 七牛上傳圖片出現(xiàn)文件已存在的錯誤(file exists)3. css3 - 圖片等比例縮放4. javascript - QWebEngineView 如何爬 angular 的動態(tài)數(shù)據(jù)?5. javascript - 使用angular 的ui-sref 中出現(xiàn)了中文參數(shù),點擊跳轉(zhuǎn)后瀏覽器的地址欄里出現(xiàn)轉(zhuǎn)義后的%AE....%a%46. html5 - 這個代碼顯示功能如何實現(xiàn)?7. html - css3中多列高度 統(tǒng)一8. java - 郵箱如何發(fā)送html內(nèi)容9. css3 - animation屬性,safari瀏覽器不支持相關(guān)效果10. java - ConcurrentHashMap中的get()方法為什么可以不加鎖?
