A fast python HTML parser [closed] A fast python HTML parser [closed] xml xml

A fast python HTML parser [closed]


Streaming (or SAX-style) parsers can be faster than DOM-style ones. Your code is passed elements one at a time as they occur in the document, and although you have to infer (and keep track of) their relationships yourself, you only need to maintain as much state as is required to locate the data you want. As a bonus, once you've found what you're interested in, you can terminate parsing early, saving the time that would have been required to process the rest of the document.

In contrast, DOM-style parsers need to build a complete navigable object model of the whole document, which takes time (and memory). DOM-style parsers are typically built on top of streaming parsers, so they will ceteris paribus be slower than the streaming parser they use.

Python has a streaming parser for HTML called html.parser. Depending on how hard it is to recognize the data you want to extract, it can be complicated to actually program a streaming parser to do scraping, because the API is sort of inside-out from the way you're used to thinking of documents. So it may be worth choosing an easier-to-use parser even if it's slower at runtime, because simple code that works is generally better than complicated code with bugs.

On the gripping hand, a parser written in C (such as lxml) is going to blow the doors off pretty much any parser written in pure Python, regardless of what approach it takes, so that might be a way to get the speed you need. (In fact, these days, BeautifulSoup uses lxml as its default parser.)


try: ElementTree could be faster, but i am not sure.

xml.etree.ElementTree import ElementTree