[英]Beautiful soup some errors
所以,我為亞馬遜制作了一個 web 刮板,只是為了一個人事項目,但我遇到了一個問題,即每當我使用 get_text 時,它都會顯示屬性錯誤,但它在我所指的視頻中工作得非常好,我沒有得到它。 在我沒有使用 header 之前,它讓我覺得它可能有問題,所以我復制了它,因為它是教練在視頻教程中寫的。
import requests
from bs4 import BeautifulSoup
URL="https://www.amazon.in/dp/B074WZJ4MF/ref=redir_mobile_desktop?_encoding=UTF8&aaxitk=8bc2212eee66e1c1bdca057df16f612f&hsa_cr_id=2722802130102&pd_rd_plhdr=t&pd_rd_r=135b3806-45ad-402d-9df7-0f14d458f874&pd_rd_w=19o2S&pd_rd_wg=TBmei&ref_=sbx_be_s_sparkle_mcd_asin_0_title"
HEADERS={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
def getprice():
page= requests.get(URL, headers=HEADERS)
# print(htmlcontent)
soup=BeautifulSoup(page.content,'html.parser')
# print(soup.prettify)
title=soup.find(id="productTitle").get_text()
print(title)
if __name__=="__main__":
getprice()
這是代碼:IDK 為什么會這樣,讓我也向您展示 output: Output
該鏈接只是一個隨機獲取的鏈接,獲取的 id 是我希望它顯示的產品的標題。 請幫助我在整個互聯網上搜索它。
您的HEADERS
變量是一個字典。 您應該正確設置 User-Agent 密鑰。
HEADERS={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
如果您正在尋找簡單的方法來獲得解決方案。 您可以使用 selenium 進行刮擦
這是代碼。
driver= webdriver.Chrome("C:/chromedriver.exe")
url='https://....."
driver.get(url)
price= driver.find_element_by_xpath("//span[@class='a-price a-text-price a-size-m
edium apexPriceToPay']").text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.