我正在创建一个程序“构建一个追踪亚马逊价格的Python应用程序!”但当我运行该程序时,它显示ValueError:无法将字符串转换为浮点数:'2.283.00 - 5.331.00‘。我试着寻找解决方案,但都没有奏效。下面是我的代码:
import requests
from bs4 import BeautifulSoup
import smtplib
URL = "https://www.amazon.in/Nike-Trainer-Training-Shoes-8-924206-060/dp/B07FKG4SGQ/ref=sr_1_2?
dchild=1&keywords=nike+shoes+for+men&qid=1609408052&sr=8-2"
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/87.0.4280.88 Safari/537.36'}
def check_price():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text(strip=True)
price = soup.find(id="priceblock_ourprice").get_text()
converted_price = float(price.replace('₹\xa0' , '').replace(',', '.'))
print(title)
print(converted_price)
if (converted_price > 4000):
send_mail()
def send_mail():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('aryan111san@gmail.com', 'ezrdsmxfokoeihlb')
subject = 'Price fell down!'
body = 'check flipkart link https://www.amazon.in/Nike-Trainer-Training-Shoes-8-924206-
060/dp/B07FKG4SGQ/ref=sr_1_2?dchild=1&keywords=nike+shoes+for+men&qid=1609408052&sr=8-2'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
'aryan111san@gmail.com',
'aryaditya007@gmail.com',
msg
)
print("Email has been sent!")
server.quit()
check_price()
发布于 2020-12-31 19:00:06
显而易见的答案是'2.283.00 - 5.331.00‘不是可以转换为浮点数的东西。它需要一个类似于'1.23‘的格式。
首先它是一个范围,所以你需要在‘-’上把它分成两个数字。
prices = soup.find(id="priceblock_ourprice").get_text().split(' - ')
然后将每个价格上的千位分隔符',‘替换为'’:
price.replace('₹\xa0' , '').replace(',', '')
更好的选择可能是导入语言环境并使用locale.atof()转换每个价格(不带货币符号)。
发布于 2020-12-31 19:00:06
'2.283.00 - 5.331.00‘显然不是单一价格,因此不能转换为浮点数。该格式似乎是价格的一个范围,例如,price_from和price_to。您应该首先拆分并转换其中的每一个。一种简单的方法是通过正则表达式。然后修改check_price()
,如下所示:
import re
def check_price():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text(strip=True)
price = soup.find(id="priceblock_ourprice").get_text()
# Replace comma with empty string to allow float conversion: float('2283.00') is fine, float('2,283.00') is not
price = price.replace(',', '')
# Use regular expression to identify prices.
# If a single price (eg € 2283.00), then prices = [('2283.00', '.00')]
# If range (eg € 2283.00 - € 5331.00), then prices = [('2283.00', '.00'), ('5331.00', '.00')]
prices = re.findall(r'(\d+(\.\d+)?)', price)
# Iterate over the prices to do some action
for price, decimals in prices:
print(float(price))
if (float(price) > 4000):
send_mail()
发布于 2020-12-31 19:00:28
正如你可以看出2.283.00
不是一个数字,它是从2,283.00
得到的,python不容易解析,但你知道在python中可以使用下划线来分隔千吗?所以2_283.00
在python中是一个数字。此外,您正在尝试解析一个数字范围,您应该首先拆分它们
只要改变就好
converted_price = float(price.replace('₹\xa0' , '').replace(',', '.'))
至
converted_price = [float(item) for item in (price.replace('₹\xa0' , '').replace(',', '_').split()[slice(0,3,2)])]
converted_price
现在是一个包含2个项目的列表,选择你想要的项目或对它们执行任何你想要的数学运算(例如,获得平均值)
https://stackoverflow.com/questions/65519947
复制相似问题