0

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!

W.creator
  • 13
  • 1
  • 6
  • Quite helpful as well... https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something – stamstam Apr 26 '21 at 12:44

2 Answers2

3

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"}

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

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"})
norok2
  • 25,683
  • 4
  • 73
  • 99
  • Hi norok and DeepSpace, unfortunately your suggestions didnt work for some reason. Here is a screen shot of how I obtained the information: [link](http://imageshack.com/a/img921/4307/80dems.png) also here are some details of the packages and versions installed on my system...beautifulsoup4-4.5.0.dist-info bs4 bs4-0.0.1.dist-info. If BeatuifulSoup is not so good is there other way I can extract the necessary info from the site? – W.creator Aug 02 '16 at 12:29
  • `result = soup.find('td', **{'class': "rightCol"})` this? or better: `result = soup.find('td', class='rightCol')` this? or probably, given the HTML you want to parse.. `result = soup.find_all('td', class='rightCol')` ? – norok2 Aug 02 '16 at 12:41
  • Hi norok2. Thanks for the suggestions but all three of them return an "invalid syntax" error in IDLE3 and don't allow the program to run at all. – W.creator Aug 02 '16 at 13:01
  • the above examples were from the original documentation. You should make sure to have access to the documentation of your version and follow the syntax accordingly... – norok2 Aug 02 '16 at 13:11