當我在程序中使用float()
方法時,出現錯誤。你能幫我解決這個問題嗎?我正在使用python 3.4.0a4。python中的回溯錯誤
這是程序:
import urllib.request
price = 99.99
while price > 4.74:
page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
price = float(text[start_of_price:end_of_price])
Print("Buy!")
,這是錯誤我得到:
Traceback (most recent call last):
File "F:/Python/python 8.py", line 11, in <module>
price = float(text[start_of_price:end_of_price])
ValueError: could not convert string to float: '!DOC'
您發現錯誤的'> $'。如果您正在解析HTML,請考慮使用HTML解析器,例如BeautifulSoup。 –
除非你預先定義了'Print',否則你會在最後一行得到'NameError',因爲它應該是'print'。請記住Python區分大小寫。 – iCodez
您的切片字符串「text [start_of_price:end_of_price]」不是數字。 – aIKid