微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Scrapy请求得到一些响应,但不是全部

我正在抓取一个页面,该页面在相同的

我正在使用这种方式: 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

解决方法

似乎部分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']
  ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。