Python: Parse HTML using Beautiful Soup

Categories: Python; Tagged with: ; @ April 6th, 2014 17:24


Get the FX rate from:


Use urllib2 to get the html, use BeautifulSoup to parse the html;


1.  Get html by urllib2:

import urllib2
html = urllib2.urlopen(“”).read()

2. parse the html using BeautifulSoup:

install it:

$ apt-get install python-bs4 //

Python Code:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)   # you may check the HTML by: print(soup.prettify()) 
sgRateString = soup.find(id=”realRateInfo”).find_all(‘tr’)[8].find_all(‘td’)[5].get_text()
sgRate = float(sgRateString)
print ‘SG Rate:’, sgRate

All Code:


urllib2 examples:

BeautifulSoup Quick Start:


// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.