Skip to content


BSD 56 5 2,262
2.3 million (month) Dec 05 2008 2.0.0(1 year, 2 months ago)
1,431 5 36 MIT
2.8.1(9 months ago) Jun 01 2013 147.7 thousand (month)

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.

HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has well over five million downloads.

HTML5 provides the following features:

  • An HTML5 serializer
  • Support for PHP namespaces
  • Composer support
  • Event-based (SAX-like) parser
  • A DOM tree builder
  • Interoperability with QueryPath
  • Runs on PHP 5.3.0 or newer

Note that html5-php is a low-level HTML parser and does not feature any query features like CSS selectors.



Example Use

from pyquery import PyQuery as pq

# this is our HTML page:
html = """
  <title>Hello World!</title>
  <div id="product">
    <h1>Product Title</h1>
    <p>paragraph 1</p>
    <span class="price">$10</span>

doc = pq(html)

# we can use CSS selectors:
print(doc('#product .price').text())

# it's also possible to modify HTML tree in various ways:
# insert text into selected element:
"<h1>Product Title<span>discounted</span></h1>"

# or remove elements
<h1>Product Title<span>discounted</span></h1>
<span class="price">$10</span>

# pyquery can also retrieve web documents using requests:
doc = pq(url='', headers={"User-Agent": ""})
// Assuming you installed from Composer:
require "vendor/autoload.php";

use Masterminds\HTML5;

// An example HTML document:
$html = <<< 'HERE'
  <body id='foo'>
    <h1>Hello World</h1>
    <p>This is a test of the HTML5 parser.</p>

// Parse the document. $dom is a DOMDocument.
$html5 = new HTML5();
$dom = $html5->loadHTML($html);

// Render it as HTML5:
print $html5->saveHTML($dom);

// Or save it to a file:
$html5->save($dom, 'out.html');

Alternatives / Similar