簡體   English   中英

python,requests和bs4的亞馬遜價格網頁抓取問題

[英]amazon price web scraping problem with python, requests and bs4

因此,我編寫了一段代碼來獲取有關亞馬遜產品的信息,並且使我可以獲取價格並設置條件。如果滿足價格條件,我將使用gmail向自己發送一條消息。問題是當我使用代碼獲取價格時

'NoneType' object has no attribute 'get_text'

這是我的代碼。 不是全部內容,而是僅收集有關產品的信息:

url="https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}
page=requests.get(url,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id="productTitle").get_text()
price=soup.find(id="priceblock_ourprice").get_text()
converted_price=float(price[0:6])
print(converted_price)
print(title.strip())

在您抓取的頁面中找不到priceblock_ourprice

from bs4 import BeautifulSoup
import requests

url = "https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find('span', {'id': 'productTitle'}).text
print(title.strip())

只需將用戶代理更改為Mozilla,然后嘗試以下代碼。

url="https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
headers={"User-Agent":"Mozilla/5.0"}
page=requests.get(url,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id="productTitle").get_text()
price=soup.find(id="priceblock_ourprice").get_text()
price=price.replace(',','')
converted_price=float(price[0:6])
print(converted_price)
print(title.strip())

輸出:

1.9213
Sony Alpha 7M3 E-Mount Vollformat Digitalkamera ILCE-7M3 (24,2 Megapixel, 7,6cm (3 Zoll) Touch-Display, Exmor R CMOS Vollformatsensor, XGA OLED Sucher, 2 Kartenslots, nur Gehäuse) schwarz

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM