tags:

views:

5111

answers:

8

Using the Python Documentation I found the HTML parser but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).

+3  A: 

Try:

import HTMLParser
Koh Wei Jie
+1  A: 

There's a link to an example on the bottom of the page.

Vytautas Shaltenis
+12  A: 

You probably really want BeautifulSoup, check the link for an example.

But in any case

>>> import HTMLParser
>>> h = HTMLParser.HTMLParser()
>>> h.feed('<html></html>')
>>> h.get_starttag_text()
'<html>'
>>> h.close()
Vinko Vrsalovic
+2  A: 

I would recommend using Beautiful Soup module instead and it has good documentation.

Swaroop C H
I'm gonna give it a whirl, thanks for the suggestion
Teifion
+1  A: 

For real world HTML processing I'd recommend BeautifulSoup. It is great and takes away much of the pain. Installation is easy.

Antti Rasinen
+1  A: 

You should also look at html5lib for Python as it tries to parse HTML in a way that very much resembles what web browsers do, especially when dealing with invalid HTML (which is more than 90% of today's web).

Alexey Feldgendler
Please add a link to html5lib. Thank you.
Cristian Ciupitu
+2  A: 

I don't recommend BeautifulSoup if you want speed. lxml is much, much faster, and you can fall back in lxml's BS soupparser if the default parser doesn't work.

Koh Wei Jie
I agree, BeautifulSoup is only useful when parsing a handful of files, there are too many memoryleaks.
DrDee
+2  A: 

You may be interested in lxml. It is a separate package and has C components, but is the fastest. It has also very nice API, allowing you to easily list links in HTML documents, or list forms, sanitize HTML, and more. It also has capabilities to parse not well-formed HTML (it's configurable).

phjr