[Tutor] using BeautifulSoup

Kent Johnson kent37 at tds.net
Mon Mar 27 18:13:56 CEST 2006


jonasmg at softhome.net wrote:
> Hi! 
> 
> I'm trying to use BeautifulSoup for get data from a table (on right) from:
> http://en.wikipedia.org/wiki/United_states 
> 
> i.e. i would get data from 'Calling code' that it would be '+1' 
> 
>  ---------------------- 
> 
> import urllib2
> from BeautifulSoup import BeautifulSoup 
> 
> url="http://en.wikipedia.org/wiki/United_states"
> html = urllib2.urlopen(url).read()
> soup = BeautifulSoup()
> soup.feed(html) 

You just have to find some kind of ad hoc search that gets you to where 
you want to be. I would try something like this:

anchor = soup.fetch('a', dict(href="/wiki/List_of_country_calling_codes"))

code = anchor.findNext('code')
print code.string

Presumably you want this to work for other country pages as well; you 
will have to look at the source, see what they have in common and search 
on that.

Kent



More information about the Tutor mailing list