我目前无法测试,所以请告诉我这是否对你有用,因此如果有任何错误,我可以对其进行编辑。
这里的想法是,我们在首页中找到每个链接,并通过将你的产品解析方法作为回调传递新的scrapy请求
import scrapyfrom urllib.parse import urljoinclass ProductsSpider(scrapy.Spider): name = "products" start_urls = [ 'https://www.phidgets.com/?tier=1&catid=64&pcid=57', ] def parse(self, response): products = response.xpath("//*[contains(@class, 'ph-summary-entry-ctn')]/a/@href").extract() for p in products: url = urljoin(response.url, p) yield scrapy.Request(url, callback=self.parse_product) def parse_product(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(), }


