我正在尝试创建一个网络爬虫,可以监测电子商务网站上的产品价格
from bs4 import BeautifulSoup
URL = "https://www.amazon.in/Apple-MacBook-Pro-9th-Generation-Intel-Core-i9/dp/B07SDPJ531/ref=sr_1_10?keywords=macbook+pro&qid=1568561733&sr=8-10"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"}
page = requests.get(URL, headers = headers )
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id = "productTitle").getText()
price = soup.find(id = "priceblock_ourprice").getText()
converted_price = float(price[1:10])
print(title.strip())
#print(price)
print(converted_price)我收到一个错误
Traceback (most recent call last):
File "C:/Users/siddi/PycharmProjects/webscraping/venv/amazonscraping.py", line 15, in <module>
converted_price = float(price[1:10])
ValueError: could not convert string to float: '\xa02,29,990'发布于 2019-09-16 00:14:45
这是一个微不足道的问题,文本包含逗号而不是.decimal,以及货币符号的unicode字符,因此您必须将它们去掉。尝试下面的代码片段
converted_price = price[1:10].replace(',','.') # replace , with .
converted_price = ''.join([ch for ch in converted_price if ch in '0123456789.')]) # remove everything except decimal and digits
converted_price = float(converted_price) # now do the convertionhttps://stackoverflow.com/questions/57945885
复制相似问题