简体   繁体   中英

amazon price web scraping problem with python, requests and bs4

So I have written a piece of code that gets information about a product in amazon and I have made it possible to get the price and set a condition.If the condition of the price is met I will send a message using gmail to myself.The problem is when I use the code to get the price it says

'NoneType' object has no attribute 'get_text'

This is my code. Not the full part of it, but only gathering information about the product:

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())

I could not find priceblock_ourprice in the page you are scraping with.

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())

Just change the user-agent to Mozilla only and try the below code.

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())

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM