Skip to content


MIT 62 3 213
671.7 thousand (month) Apr 20 2015 1.3.6(7 months ago)
2,262 5 56 BSD
2.0.0(1 year, 2 months ago) Dec 05 2008 2.3 million (month)

The xml2 package is a binding to libxml2, making it easy to work with HTML and XML from R. The API is somewhat inspired by jQuery.

xml2 can be used to parse HTML documents using XPath selectors and is a successor to R's XML package with a few improvements:

  • xml2 takes care of memory management for you. It will automatically free the memory used by an XML document as soon as the last reference to it goes away.
  • xml2 has a very simple class hierarchy so don't need to think about exactly what type of object you have, xml2 will just do the right thing.
  • More convenient handling of namespaces in Xpath expressions - see xml_ns() and xml_ns_strip() to get started.

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.



Example Use

x <- read_xml("<foo> <bar> text <baz/> </bar> </foo>")

xml_find_all(x, ".//baz")

h <- read_html("<html><p>Hi <b>!")
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": ""})

Alternatives / Similar