Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: how to get text from a html file?

2 views
Skip to first unread message

Chris Colbert

unread,
Apr 13, 2010, 2:12:01 PM4/13/10
to varnikat t, python mailing
On Tue, Apr 13, 2010 at 1:58 PM, varnikat t <varni...@gmail.com> wrote:
>
> Hi,
> Can anyone tell me how to get text from a html file?I am trying to display
> the text of an html file in textview(of glade).If i directly display the
> file,it shows with html tags and attributes, etc. in textview.I don't want
> that.I just want the text.
> Can someone help me with this?
>
>
> Regards
> Varnika Tewari
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

You should look into beautiful soup

http://www.crummy.com/software/BeautifulSoup/

Grant Edwards

unread,
Apr 13, 2010, 2:22:13 PM4/13/10
to
On Tue, Apr 13, 2010 at 1:58 PM, varnikat t <varni...@gmail.com> wrote:

> Can anyone tell me how to get text from a html file?I am trying to display
> the text of an html file in textview(of glade).If i directly display the
> file,it shows with html tags and attributes, etc. in textview.I don't want
> that.I just want the text.

[Parent article is unavailable on gmane, so my reply isn't quite in
the right place in the tree]

I generally just use something like this:

Popen(['w3m','-dump',filename],stdout=PIPE).stdout.read()

I'm sure there are more complex ways...

--
Grant Edwards grant.b.edwards Yow! I'm having fun
at HITCHHIKING to CINCINNATI
gmail.com or FAR ROCKAWAY!!

Stefan Behnel

unread,
Apr 13, 2010, 2:26:07 PM4/13/10
to pytho...@python.org
varnikat t, 13.04.2010 19:58:

> Can anyone tell me how to get text from a html file?I am trying to display
> the text of an html file in textview(of glade).If i directly display the
> file,it shows with html tags and attributes, etc. in textview.I don't want
> that.I just want the text.
> Can someone help me with this?

E.g. using lxml.html:

import lxml.html as H
html = H.parse("the_html_file.html")
print H.tostring(html, method="text")

Stefan

rake

unread,
Apr 13, 2010, 8:45:09 PM4/13/10
to
On Apr 13, 2:12 pm, Chris Colbert <sccolb...@gmail.com> wrote:

For more complex parsing beautiful soup is definitely the way to go.

However, if all you want to do is strip the html and keep all
remaining text I'd recommend pyparsing package with this short script:

http://pyparsing.wikispaces.com/file/view/htmlStripper.py

Stefan Behnel

unread,
Apr 14, 2010, 2:43:36 AM4/14/10
to pytho...@python.org
rake, 14.04.2010 02:45:

> On Apr 13, 2:12 pm, Chris Colbert wrote:
>> You should look into beautiful soup
>>
>> http://www.crummy.com/software/BeautifulSoup/
>
> For more complex parsing beautiful soup is definitely the way to go.

Why would a library that even the author has lost interest in be "the way
to go"?

Stefan

Emile van Sebille

unread,
Apr 14, 2010, 9:24:22 AM4/14/10
to pytho...@python.org
On 4/13/2010 11:43 PM Stefan Behnel said...
> rake, 14.04.2010 02:45:

>> On Apr 13, 2:12 pm, Chris Colbert wrote:
>>> You should look into beautiful soup
>>>
>>> http://www.crummy.com/software/BeautifulSoup/
>>
>> For more complex parsing beautiful soup is definitely the way to go.
>
> Why would a library that even the author has lost interest in be "the
> way to go"?
>
> Stefan
>
Why not when the recent release dates from only five days ago?

Emile

Grant Edwards

unread,
Apr 14, 2010, 9:29:57 AM4/14/10
to

Sure, if the library is still being maintained. I can't think of too
many open-source projects where somebody else hasn't taken over from
the original author.

--
Grant Edwards grant.b.edwards Yow! I'm dressing up in
at an ill-fitting IVY-LEAGUE
gmail.com SUIT!! Too late...

Stefan Behnel

unread,
Apr 14, 2010, 10:00:52 AM4/14/10
to pytho...@python.org
Emile van Sebille, 14.04.2010 15:24:

> On 4/13/2010 11:43 PM Stefan Behnel said...
>> rake, 14.04.2010 02:45:
>>> On Apr 13, 2:12 pm, Chris Colbert wrote:
>>>> You should look into beautiful soup
>>>>
>>>> http://www.crummy.com/software/BeautifulSoup/
>>>
>>> For more complex parsing beautiful soup is definitely the way to go.
>>
>> Why would a library that even the author has lost interest in be "the
>> way to go"?
>
> Why not when the recent release dates from only five days ago?

Interesting, even the web site has had a revamp.

Nice - I like competition. ;)

Stefan

0 new messages