如何解决将“数字字符串”转换为浮点数据类型时的错误

时间:2019-09-15 16:09:29

标签: python web-scraping beautifulsoup python-requests

我正在尝试创建一个Web爬网程序,该爬网程序可以监视电子商务网站上的产品价格

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'

1 个答案:

答案 0 :(得分:2)

这是一个小问题,文本包含。[逗号]而不是。[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 convertion