我正在抓取一個在同一個 <@div (bold) xpath 中有 36 個 <@hrefs 的頁面,所以當(dāng)我嘗試獲取這些內(nèi)容時,即使在 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條經(jīng)驗 獲得超7個贊
看來html的一部分是動態(tài)加載的,所以scrapy看不到它。數(shù)據(jù)本身存在于 html 中的 json 結(jié)構(gòu)中。你可以嘗試這樣獲?。?/p>
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']
...
添加回答
舉報
0/150
提交
取消