Reputation: 13
I am trying to create a very simple Python currency converter based off the xc.com currency converter website, however at the end of my program I get the following error:
line 16, in print(result.contents[0]) AttributeError: 'NoneType' object has no attribute 'contents'
After researching similar NoneType AttributeErrors I understand that something is returning nothing but how can I find out what it is and where? Could I use the print statement earlier on in the code to find it?
Here is my code:
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])
I am using IDLE version3.5.2 on Mac OS X El Capitan v 10.11.6(15G31) I installed Python 3.5.2 through homebrew.
Here is my IDLE3 output:
Python 3.5.2 (default, Aug 2 2016, 08:10:22)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: /c2.py
Enter amount 100
Enter currency1 usd
Enter currency2 aud
Traceback (most recent call last):
File "/c2.py", line 16, in <module>
print(result.contents[0])
AttributeError: 'NoneType' object has no attribute 'contents'
>>>
Any help would be greatly appreciated,
Many thanks!
Upvotes: 0
Views: 2765
Reputation: 26896
The exception is thrown because your result
name is bind to a None
object, which is likely an indication of BeautifulSoup.find
failing for some reasons, possibly because there is ,
where I would expect a :
here:
result = soup.find('td', {'class', "rightCol"})
to be changed to this?
result = soup.find('td', {'class': "rightCol"})
Upvotes: 1
Reputation: 81614
This error is pretty straightforward, result
is None
.
result = soup.find('td', {'class', "rightCol"})
You're passing find
the set {'class', "rightCol"}
instead of the
dictionary {'class': "rightCol"}
Upvotes: 3