我正在嘗試使用以下腳本提取信任試驗網站中元素的文本(參見元素樹的影像),但它回傳的只是一堆“無”。
url = "https://uk.trustpilot.com/review/rockar.com"
try_url = requests.get(url)
soup = BeautifulSoup(try_url.content, 'html.parser')
print(try_url.content)
for h in soup.find_all('div', {'class': 'styles_reviewContent__3TSDf'}):
hdln = h.find("h2")
print(hdln)
解決這個問題的方法是什么?我看錯了選擇器嗎?

uj5u.com熱心網友回復:
正如@diggusbickus 指出的那樣,您可以通過以下方式獲得評論:
data = json.loads(soup.find('script', type='application/json').string)
reviews = data["props"]["pageProps"]["reviews"]
sample_reply = reviews[0]["reply"]
的sample_reply是
{'message': "Thank you so much for your kind words, Fi! It's great to hear Shah was fantastic and offer a personal service to your car buying journey. Thank you for taking the time to leave us a great review! We hope you love your new vehicle! Thanks again for choosing Rockar :-)",
'publishedDate': '2021-11-04T12:35:25.401Z',
'updatedDate': '2021-11-04T12:35:34.948Z'}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354335.html
上一篇:如何從網頁中拉入表格?
