Skip to main content

HTML Parsing for Humans.

Project description

Requests-HTML: HTML Parsing for Humans™

This library intends to make parsing HTML (e.g. scraping the web) as simple and intuitive as possible.

When using this library you automatically get:

  • CSS Selectors (a.k.a jQuery-style, thanks to PyQuery).

  • XPath Selectors, for the faint at heart.

  • Mocked user-agent (like a real web browser).

  • Automatic following of redirects.

  • Connection–pooling and cookie persistience.

  • The Requests experience you know and love, with magical parsing abilities.

Usage

Make a GET request to ‘python.org’, using Requests:

>>> import requests_html
>>> session = requests_html.Session()

>>> r = session.get('https://python.org/')

Grab a list of all links on the page, as–is (anchors excluded):

>>> r.html.links
{'/users/membership/', '/about/gettingstarted/', 'http://feedproxy.google.com/~r/PythonInsider/~3/zVC80sq9s00/python-364-is-now-available.html', '/about/success/', 'http://flask.pocoo.org/', 'http://www.djangoproject.com/', '/blogs/', ... '/psf-landing/', 'https://wiki.python.org/moin/PythonBooks'}

Grab a list of all links on the page, in absolute form (anchors excluded):

>>> r.html.absolute_links
{'http://feedproxy.google.com/~r/PythonInsider/~3/zVC80sq9s00/python-364-is-now-available.html', 'https://www.python.org/downloads/mac-osx/', 'http://flask.pocoo.org/', 'https://www.python.org/docs.python.org/3/tutorial/', 'http://www.djangoproject.com/', 'https://wiki.python.org/moin/BeginnersGuide', 'https://www.python.org/about/success/', 'http://twitter.com/ThePSF', 'https://www.python.org/events/python-user-group/634/', ..., 'https://wiki.python.org/moin/PythonBooks'}

Select an element with a CSS Selector:

>>> about = r.html.find('#about', first=True)

Grab an element’s text contents:

>>> print(about.text)
About
Applications
Quotes
Getting Started
Help
Python Brochure

Introspect an Element’s attributes:

>>> about.attrs
{'id': 'about', 'class': ('tier-1', 'element-1'), 'aria-haspopup': 'true'}

Select Elements within Elements:

>>> about.find('a')
[<Element 'a' href='/about/' title='' class=''>, <Element 'a' href='/about/apps/' title=''>, <Element 'a' href='/about/quotes/' title=''>, <Element 'a' href='/about/gettingstarted/' title=''>, <Element 'a' href='/about/help/' title=''>, <Element 'a' href='http://brochure.getpython.info/' title=''>]

Search for links within an element:

>>> about.absolute_links
{'http://brochure.getpython.info/', 'https://www.python.org/about/gettingstarted/', 'https://www.python.org/about/', 'https://www.python.org/about/quotes/', 'https://www.python.org/about/help/', 'https://www.python.org/about/apps/'}

Search for text on the page:

>>> r.html.search('Python is a {} language')[0]
programming

More complex CSS Selector example (copied from Chrome dev tools):

>>> r = session.get('https://github.com/')
>>> sel = 'body > div.application-main > div.jumbotron.jumbotron-codelines > div > div > div.col-md-7.text-center.text-md-left > p'

>>> print(r.html.find(sel, first=True).text)
GitHub is a development platform inspired by the way you work. From open source to business, you can host and review code, manage projects, and build software alongside millions of other developers.

XPath is also supported:

>>> r.html.xpath('a')
[<Element 'a' class='btn' href='https://help.github.com/articles/supported-browsers'>]

Other Fun (with Markdown)

If you’d like to take an element and convert it to Markdown, for example, use html2text, by Aaron Swartz:

$ pipenv install html2text
>>> from html2text import HTML2Text
>>> h = html2text.HTML2Text()
>>> print(h.handle(about.html))
* [About](/about/)

  * [Applications](/about/apps/)
  * [Quotes](/about/quotes/)
  * [Getting Started](/about/gettingstarted/)
  * [Help](/about/help/)
  * [Python Brochure](http://brochure.getpython.info/)

Installation

$ pipenv install requests-html
✨🍰✨

Only Python 3 is supported.

Project details


Download files

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

Source Distribution

requests-html-0.2.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

requests_html-0.2.1-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file requests-html-0.2.1.tar.gz.

File metadata

File hashes

Hashes for requests-html-0.2.1.tar.gz
Algorithm Hash digest
SHA256 84606bc43c406a09ec41f38cf63dfad1c2226697ca1ca1866226775b919a780e
MD5 1a9180ea672632cf8b55d8a24f6f5160
BLAKE2b-256 a9210a9f4c58c7c5495ba72b432262d7dd41afc5c2bec98ce4fdb5c465ed64d8

See more details on using hashes here.

File details

Details for the file requests_html-0.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for requests_html-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6b13bafd3bac7ff318bc7c04faeb580f1ac46f5f00fa3bccb177378cbd44da54
MD5 2f78b5609b2fb856b4a5b4d688d8d2e2
BLAKE2b-256 8b2514ecef21035fff80710debc60dd0e709375f410bf861580063d9526aeeea

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page