cari

Rumah  >  Soal Jawab  >  teks badan

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
阿神阿神2787 hari yang lalu802

membalas semua(2)saya akan balas

  • 迷茫

    迷茫2017-04-18 10:29:19

    Bina pengurus Url supaya ia tidak dirangkak berulang kali

    balas
    0
  • PHP中文网

    PHP中文网2017-04-18 10:29:19

    Faham, cuma tukar kepada ini.

    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)

    balas
    0
  • Batalbalas