为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

达令说 2023-12-08 16:44:37
我正在抓取一个在同一个 <@div (bold) xpath 中有 36 个 <@hrefs 的页面,所以当我尝试获取这些内容时,即使在 scrapy shell 上,它也始终只获得相同的 12 个 <@hrefs,并且这是不正常的。我使用这种方式:response.xpath('/html/body/div[1]/div[2]/section/div/div[3]/div[2]/div/div[2]// div //article//div[1]// a[re:test(@href,"pd")]//@href').getall()它来自以下页面: https://www.lowes.com/pl/Bottom-freezer-refrigerators-Refrigerators-Appliances/4294789499 ?offset=36
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

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


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']

  ...


查看完整回答
反对 回复 2023-12-08
  • 1 回答
  • 0 关注
  • 37 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信