我试图创建一个非常简单的Python货币转换器,它基于xc.com网站货币兑换网站,但在我的程序结束时,我得到以下错误:
第16行,英寸 打印(结果.内容[0]) AttributeError:“NoneType”对象没有属性“contents”
在研究了类似的非类型属性错误之后,我明白了某些东西什么也没有返回,但是我如何才能找到它是什么,在哪里呢?我可以用前面代码中的print语句来找到它吗?在
这是我的代码:
import requests
from bs4 import BeautifulSoup
amount = input("Enter amount ")
currency1 = input("Enter currency1 ")
currency2 = input("Enter currency2 ")
url = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" +
amount + "&From=" + currency1 + "&To=" + currency2
html_code = requests.get(url).text
soup = BeautifulSoup(html_code, "html.parser")
result = soup.find('td', {'class', "rightCol"})
print(result.contents[0])
我在Mac OS X El Capitan v 10.11.6(15G31)上使用空闲版本3.5.2 我通过自制程序安装了python3.5.2。在
以下是我的IDLE3输出:
^{pr2}$任何帮助都将不胜感激
非常感谢!在
这个错误很简单,
result
是None
。在您传递的是
find
集合{'class', "rightCol"}
,而不是字典
{'class': "rightCol"}
引发异常是因为您的
result
名称绑定到None
对象,这很可能是由于某些原因,BeautifulSoup.find
失败的迹象,可能是因为这里有一个,
的地方:改成这样?在
^{pr2}$相关问题 更多 >
编程相关推荐