在scrapy中使用for循环的多个url

2024-10-01 09:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从多个网址抓取信息。我使用下面的代码,但它不工作。有人能告诉我哪里出了问题吗?你知道吗

import scrapy


class spider1(scrapy.Spider):
    name = "spider1"
    domain = "http://www.amazon.com/dp/"
    ASIN = ['B01LA6171I', 'B00OUKHTLO', 'B00B7LUVZK']

    def start_request(self):
        for i in ASIN:
            yield scrapy.Request(url=domain+i, callback=self.parse)

    def parse(self, response):
        title = response.css("span#productTitle::text").extract_first().strip()
        ASIN_ext = response.xpath("//input[@name='ASIN']/@value").extract_first()
        data = {"ASIN": ASIN_ext, "title": title, }
        yield data

Tags: nameselfdatatitleparseresponsedomaindef