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

我正在抓取一个在同一个 <@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


达令说
浏览 51回答 1
1回答

吃鸡游戏

看来html的一部分是动态加载的,所以scrapy看不到它。数据本身存在于 html 中的 json 结构中。你可以尝试这样获取:import json# get the script with the datajson_data = response.xpath('//script[contains(text(), "__PRELOADED_STATE__")]/text()').extract_first()# load the data in a python dictionarydict_data = json.loads(json_data.split('window.__PRELOADED_STATE__ =')[-1])items = dict_data['itemList']print(len(items))&nbsp; # prints 36 in my case# go through the dictionary and get the product_urlsfor item in items:&nbsp; product_url = item['product']['pdURL']&nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python