Skip to main content

XML Parsing for humans.

Project description

Requests-XML: XML Parsing for Humans

.. image::
.. image::
.. image::

This library intends to make parsing XML as
simple and intuitive as possible. **Requests-XML** is related
to the amazing `Requests-HTML <>`_
and delivers the same quality of user experience — with support for our beloved XML documents.

When using this library you automatically get:

- *XPath Selectors*, for the *brave* at heart.
- *Simple Search/Find* for the *faint* at heart.
- XML to JSON conversion thanks to `xmljson <>`_.
- Mocked user-agent (like a real web browser).
- Connection–pooling and cookie persistence.
- The Requests experience you know and love, with magical XML parsing abilities.


.. code-block:: shell

$ pipenv install requests-xml

Only **Python 3.6** is supported.

Tutorial & Usage

Make a GET request to ` <>`_, using `Requests <>`_:

.. code-block:: pycon

>>> from requests_xml import XMLSession
>>> session = XMLSession()

>>> r = session.get('')

Grab a list of all links on the page, as–is (this only works for RSS feeds, or other feeds that happen to have `link` elements):

.. code-block:: pycon

>>> r.xml.links
['', '', '', '', ...]

XPath is the main supported way to query an element (`learn more <>`_):

.. code-block:: pycon

>>> item = r.xml.xpath('//item', first=True)
<Element 'item' >

Grab an element's text contents:

.. code-block:: pycon

>>> print(item.text)
The Beauty of Light
The Soyuz MS-08 rocket is launched with Soyuz Commander Oleg Artemyev of Roscosmos and astronauts Ricky Arnold and Drew Feustel of NASA, March 21, 2018, to join the crew of the Space Station.
Wed, 21 Mar 2018 14:12 EDT
NASA Image of the Day

Introspect an element's attributes (`learn more <>`_):

.. code-block:: pycon

>>> rss = r.xml.xpath('//rss', first=True)
>>> rss.attrs
{'version': '2.0', '{}base': ''}

Render out an element's XML (note: namespaces will be applied to sub elements when grabbed):

.. code-block:: pycon

>>> item.xml
'<item xmlns:atom="" xmlns:dc="" xmlns:itunes="" xmlns:media=""> <title>The Beauty of Light</title>\n <link></link>\n <description>The Soyuz MS-08 rocket is launched with Soyuz Commander Oleg Artemyev of Roscosmos and astronauts Ricky Arnold and Drew Feustel of NASA, March 21, 2018, to join the crew of the Space Station.</description>\n <enclosure url="" length="1267028" type="image/jpeg"/>\n <guid isPermaLink="false"></guid>\n <pubDate>Wed, 21 Mar 2018 14:12 EDT</pubDate>\n <source url="">NASA Image of the Day</source>\n</item>'

Select an element list within an element:

.. code-block:: pycon

>>> item.xpath('//enclosure')[0].attrs['url']

Search for links within an element:

.. code-block:: pycon

>>> item.links

Search for text on the page. This is useful if you wish to search out things between specific tags without using XPath:

.. code-block:: pycon

<Result ('NASA Image of the Day',) {}>

Using PyQuery we can use CSS selectors to easily grab an element, with a simple syntax for ensuring the element
contains certain text. This can be used as another easy way to grab an element without an XPath:

.. code-block:: pycon

>>> light_title = r.xml.find('title', containing='The Beauty of Light')
[<Element 'title' >]

>>> light_title[0].text
'The Beauty of Light'

Note: XPath is preferred as it can allow you to get very specific with your element selection. Find is intended to be
an easy way of grabbing all elements of a certain name. Find does however accept CSS selectors, and if you can get those
to work with straight XML, go for it!

JSON Support

Using the great `xmljson <>`_ package, we convert the whole
XML document into a JSON representation. There are six different conversion convetions available.
See the `about <>`_ for what they are. The default is ``badgerfish``.
If you wish to use a different conversion convention, pass in a string with the name of the convetion to the
``.json()`` method.

Using without Requests

You can also use this library without Requests:

.. code-block:: pycon

>>> from requests_xml import XML
>>> doc = """
<name value="Alice"/>
<name value="Bob"/>

>>> xml = XML(xml=doc)
>>> xml.json()
"employees": [{
"person": {
"name": {
"@value": "Alice"
}, {
"person": {
"name": {
"@value": "Bob"


Project details

Release history Release notifications

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
requests_xml-0.2.3-py2.py3-none-any.whl (11.5 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Mar 24, 2018
requests-xml-0.2.3.tar.gz (9.9 kB) Copy SHA256 hash SHA256 Source None Mar 24, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page