Scrapy 请求得到一些响应,但不是全部

Scrapy request get some responses, but not all

我正在抓取一个在同一个 <@div(粗体)xpath 中有 36 个 <@href 的页面,所以当我尝试获取这些时,即使是在 scrapy shell 上,它一直只得到相同的 12 个 <@hrefs,而且顺序不对。

我是这样使用的: response.xpath('/html/body/div[1]/div[2]/section/div/div[3]/div[2]/div/div[2]/ /div//文章//div[1]// a[re:test(@href,"pd")]//@href').getall()

它来自以下页面: https://www.lowes.com/pl/Bottom-freezer-refrigerators-Refrigerators-Appliances/4294789499?offset=36

好像html的一部分是动态加载的,所以scrapy看不到。数据本身存在于 html 中的 json-structure 中。你可以尝试这样获取:

import json
# get the script with the data
json_data = response.xpath('//script[contains(text(), "__PRELOADED_STATE__")]/text()').extract_first()
# load the data in a python dictionary
dict_data = json.loads(json_data.split('window.__PRELOADED_STATE__ =')[-1])
items = dict_data['itemList']
print(len(items))  # prints 36 in my case
# go through the dictionary and get the product_urls
for item in items:
  product_url = item['product']['pdURL']
  ...