使用scrapy获取url列表,然后在这些url中搜索内容

2024-05-19 08:11:56 发布

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

我需要一个Scrapy spider来为每个URL(30个产品,所以30个URL)刮下下面的页面(https://www.phidgets.com/?tier=1&catid=64&pcid=57),然后通过该URL进入每个产品并刮下其中的数据。

我让第二部分完全按照我的意愿工作:

import scrapy

class ProductsSpider(scrapy.Spider):
    name = "products"
    start_urls = [
        'https://www.phidgets.com/?tier=1&catid=64&pcid=57',
    ]

    def parse(self, response):
        for info in response.css('div.ph-product-container'):
            yield {
                'product_name': info.css('h2.ph-product-name::text').extract_first(),
                'product_image': info.css('div.ph-product-img-ctn a').xpath('@href').extract(),
                'sku': info.css('span.ph-pid').xpath('@prod-sku').extract_first(),
                'short_description': info.css('div.ph-product-summary::text').extract_first(),
                'price': info.css('h2.ph-product-price > span.price::text').extract_first(),
                'long_description': info.css('div#product_tab_1').extract_first(),
                'specs': info.css('div#product_tab_2').extract_first(),
            }

        # next_page = response.css('div.ph-summary-entry-ctn a::attr("href")').extract_first()
        # if next_page is not None:
        #     yield response.follow(next_page, self.parse)

但我不知道怎么做第一部分。如您所见,我已将主页(https://www.phidgets.com/?tier=1&catid=64&pcid=57)设置为起始url。但是我如何让它用我需要爬网的所有30个url填充start_url列表呢?


Tags: httpsdivinfocomurlresponsewwwextract

热门问题