Skip to content

html5libvspyquery

MIT 97 14 1,220
32.8 million (month) Jul 30 2007 1.1(2020-06-22 23:32:36 ago)
2,381 5 55 NOASSERTION
Dec 05 2008 2.0 million (month) 2.0.1(2024-08-30 08:12:22 ago)

html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers.

As html5lib is implemented in pure-python it is significantly slower than alternatives powered by lxml (like parsel or beautifulsoup). However, html5lib implements a more true html5 parsing which can represent HTML tree more correctly than alternatives.

PyQuery is a Python library for working with XML and HTML documents. It is similar to BeautifulSoup and is often used as a drop-in replacement for it.

PyQuery is inspired by javascript's jQuery and uses similar API allowing selecting of HTML nodes through CSS selectors. This makes it easy for developers who are already familiar with jQuery to use PyQuery in Python.

Unlike jQuery, PyQuery doesn't support XPath selectors and relies entirely on CSS selectors though offers similar HTML parsing features like selection of HTML elements, their attributes and text as well as html tree modification.

PyQuery also comes with a http client (through requests) so it can load and parse web URLs by itself.

Highlights


css-selectors

Example Use


```python import html5lib from html5lib import parse html_doc = "My Title" parsed = parse(html_doc) title = parsed.getElementsByTagName("title")[0] print(title.childNodes[0].nodeValue) ```
```python from pyquery import PyQuery as pq # this is our HTML page: html = """ Hello World!

Product Title

paragraph 1

paragraph2

$10
""" doc = pq(html) # we can use CSS selectors: print(doc('#product .price').text()) "$10" # it's also possible to modify HTML tree in various ways: # insert text into selected element: print(doc('h1').append('discounted')) "

Product Titlediscounted

" # or remove elements doc('p').remove() print(doc('#product').html()) """

Product Titlediscounted

$10 """ # pyquery can also retrieve web documents using requests: doc = pq(url='http://httpbin.org/html', headers={"User-Agent": "webscraping.fyi"}) print(doc('h1').html()) ```

Alternatives / Similar


Was this page helpful?