Python 无法将字符串转换为浮动

Python 无法将字符串转换为浮动,python,Python,你好, 我的控制台一直在写入valueError:无法将字符串转换为浮点。 提前谢谢 在没有获得更多信息的情况下,很难回答这个问题。我假设您的错误在这一行: def check_price(): page = requests.get(URL, headers=headers) soup = BeautifulSoup(page.content, "html.parser") title = soup.find(itemprop="nam

你好,

我的控制台一直在写入valueError:无法将字符串转换为浮点。
提前谢谢

在没有获得更多信息的情况下,很难回答这个问题。我假设您的错误在这一行:

def check_price():
    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, "html.parser")

    title = soup.find(itemprop="name").get_text()
    price = soup.find(itemprop="price").get_text()
    converted_price = float(price[0:6])

    if(converted_price < 450.00):
        send_email()

    print(title.strip())
    print(converted_price)
price[0:6]是一个数组。你不能像那样把数组转换成浮点。此外,每个元素甚至可能不是一个数字,它可以是这样的:

converted_price = float(price[0:6])
现在编写一些代码,仅从中提取有用的内容。首先,您需要查看实际需要的元素,然后一旦找到该元素,就使用子字符串来获取数字。然后可以将其转换为浮点


如果这有帮助,请选择此作为解决方案

错误本身是不言自明的。你很可能得到的是一个单词而不是一个数字,Python会继续对你大喊大叫。你的问题是什么。。。这就是所有的信息吗。。。没有提到行号吗?在尝试转换之前,您需要打印标题、价格。这将告诉你问题在哪里。或者使用调试器,比如PyCharm,它是免费的!price是一个字符串,切片一个字符串会返回另一个字符串,而不是数组。
 [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
print(price)