【发布时间】:2021-03-12 09:15:33
【问题描述】:
试图从包含历史产品数据的www.archive.org 中抓取信息。我在下面的代码尝试单击列出的每个产品,scrape 每个产品的信息,并对后续页面执行相同操作。
问题是它跳过了一些产品(特别是 20 个),即使 xpath:
products = response.xpath("//article[contains(@class,'product result-prd')]")
对于所有产品都是一样的。请在下面查看我的完整代码。
class CurrysSpider(scrapy.Spider):
name = 'currys_mobiles_2015'
#allowed_domains = ['www.currys.co.uk']
start_urls = ['https://web.archive.org/web/20151204170941/http://www.currys.co.uk/gbuk/phones-broadband-and-sat-nav/mobile-phones-and-accessories/mobile-phones/362_3412_32041_xx_xx/xx-criteria.html']
def parse(self, response):
products = response.xpath("//article[contains(@class,'product result-prd')]") # done
for product in products:
brand = product.xpath(".//span[@data-product='brand']/text()").get() # done
link = product.xpath(".//div[@class='productListImage']/a/@href").get() # done
price = product.xpath(".//strong[@class='price']/text()").get().strip() # done
description = product.xpath(".//ul[@class='productDescription']/li/text()").getall() # done
absolute_url = link # done
yield scrapy.Request(url=absolute_url,callback=self.parse_product,
meta={'brand_name':brand,
'product_price':price,
'product_description':description}) # done
# process next page
next_page_url = response.xpath("//ul[@class='pagination']//li[last()]//@href").get()
absolute_next_page_url = next_page_url
if next_page_url:
yield scrapy.Request(url=absolute_next_page_url,callback=self.parse)
def parse_product(self, response):
.....
我在尝试抓取的许多网站中都注意到了这个问题,但我不确定为什么会跳过某些产品,因为所有产品列表的 xpath 都是相同的。
希望能对此提供一些反馈。
【问题讨论】:
标签: python xpath web-scraping scrapy response