1

I am trying to download/scrape the exchange rate of DKK to USD from a website. I have managed to almost get there. But this code returns more than the floating point no that I need.

#!/usr/bin/env python
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup

url = "http://www.x-rates.com/table/"
page = urllib.request.urlopen(url)
soup_packtpage = BeautifulSoup(page)
page.close()

#First, we will search for the table with class="views-view-grid" as follows:
ratestable = soup_packtpage.find("table",class_="tablesorter ratesTable")

# Find and print the value of Danish Krone to USD - only the floating point no.
print(soup_packtpage.find(text="Danish Krone").findNext('td').contents[0])

But this code returns more than the floating point no that I need. it returns this mess:

<a href="/graph/?from=USD&amp;to=DKK">7.019776</a>

Please can someone enlighten me as to how to strip the string from this floating point result, so I can store it as a variable?

Morten
  • 67
  • 1
  • 7
  • OK I fixed it myself I changed the 'td' tag to an 'a' tag, and now the code works as intended. – Morten Mar 13 '15 at 09:13

1 Answers1

0

I imagine you want another findNext('a') before your contents. You want to keep the exchange rate, as it's not a currency in itself (e.g. it's precision doesn't need to be 2sf.).

For instance:

USD      Krone
$1.00    kr.7.02
$1000    kr.7019.78

I'm not familiar with Beautiful soup but this is what I'd try.

Or potentially find a currency conversion site that uses nice class names that you could use in your search.

In fact looking at the API, this should be sufficient:

print(soup_packtpage.find(text="Danish Krone").findNext('td').string)

As string looks through all the children of a tag.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • 1
    Thanks Pureferret, This also solves a runtimeerror with my numbers RuntimeError: maximum recursion depth exceeded while getting the str of an object Now everything looks great. Thanks! – Morten Mar 13 '15 at 09:49