simple Question about using BeautifulSoup

Diez B. Roggisch deets at nospam.web.de
Wed Aug 20 16:43:50 CEST 2008


Alexnb wrote:

> 
> Okay, I have used BeautifulSoup a lot lately, but I am wondering, how do
> you open a local html file?
> 
> Usually I do something like this for a url
> 
> soup = BeautifulSoup(urllib.urlopen('http://www.website.com')
> 
> but the file extension doesn't work. So how do I open one?

The docs for urllib.urlopen clearly state that it returns a file-like
object. Which BS seems to grok.

So... how about passing another file-like object, like... *drumroll* - a
file?

soup = BeautifulSoup(open("myfile.html"))

Apart from the documented possibility to pass the html as string, which
means


soup = BeautifulSoup(open("myfile.html").read())

will work as well.

Diez



More information about the Python-list mailing list