469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

simple Question about using BeautifulSoup


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?
--
View this message in context: http://www.nabble.com/simple-Questio...p19069980.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Aug 20 '08 #1
2 5387
On 2008-08-20, Alexnb <al********@gmail.comwroted:
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?
Have you tried the local file URL, like "file:///home/user/file.html"?

GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Aug 20 '08 #2
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
Aug 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dan Stromberg | last post: by
1 post views Thread by Dan Stromberg | last post: by
4 posts views Thread by joe_public34 | last post: by
4 posts views Thread by William Xu | last post: by
3 posts views Thread by John Nagle | last post: by
2 posts views Thread by Alexnb | last post: by
1 post views Thread by Alexnb | last post: by
1 post views Thread by Jean-Paul Calderone | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.