Skip to main content

Extract text from HTML

Project description

HTML to Text

PyPI Version Build Status Code Coverage

Extract text from HTML

  • Free software: MIT license

How is html_text different from .xpath('//text()') from LXML or .get_text() from Beautiful Soup? Text extracted with html_text does not contain inline styles, javascript, comments and other text that is not normally visible to the users. It normalizes whitespace, but is also smarter than .xpath('normalize-space()), adding spaces around inline elements (which are often used as block elements in html markup), tries to avoid adding extra spaces for punctuation and can add newlines so that the output text looks like how it is rendered in browsers.

Apart from just getting text from the page (e.g. for display or search), one intended usage of this library is for machine learning (feature extraction). If you want to use the text of the html page as a feature (e.g. for classification), this library gives you plain text that you can later feed into a standard text classification pipeline. If you feel that you need html structure as well, check out webstruct library.

Install

Install with pip:

pip install html-text

The package depends on lxml, so you might need to install some additional packages: http://lxml.de/installation.html

Usage

Extract text from HTML:

>>> import html_text
>>> html_text.extract_text('<h1>Hello</h1> world!')
'Hello\n\nworld!'

>>> html_text.extract_text('<h1>Hello</h1> world!', guess_layout=False)
'Hello world!'

You can also pass already parsed lxml.html.HtmlElement:

>>> import html_text
>>> tree = html_text.parse_html('<h1>Hello</h1> world!')
>>> html_text.extract_text(tree)
'Hello\n\nworld!'

Or define a selector to extract text only from specific elements:

>>> import html_text
>>> sel = html_text.cleaned_selector('<h1>Hello</h1> world!')
>>> subsel = sel.xpath('//h1')
>>> html_text.selector_to_text(subsel)
'Hello'

Passed html will be first cleaned from invisible non-text content such as styles, and then text would be extracted. NB Selectors are not cleaned automatically you need to call html_text.cleaned_selector first.

Main functions:

  • html_text.extract_text accepts html and returns extracted text.

  • html_text.cleaned_selector accepts html as text or as lxml.html.HtmlElement, and returns cleaned parsel.Selector.

  • html_text.selector_to_text accepts parsel.Selector and returns extracted text.

If guess_layout is True (default), a newline is added before and after newline_tags, and two newlines are added before and after double_newline_tags. This heuristic makes the extracted text more similar to how it is rendered in the browser. Default newline and double newline tags can be found in html_text.NEWLINE_TAGS and html_text.DOUBLE_NEWLINE_TAGS.

It is possible to customize how newlines are added, using newline_tags and double_newline_tags arguments (which are html_text.NEWLINE_TAGS and html_text.DOUBLE_NEWLINE_TAGS by default). For example, don’t add a newline after <div> tags:

>>> newline_tags = html_text.NEWLINE_TAGS - {'div'}
>>> html_text.extract_text('<div>Hello</div> world!',
...                        newline_tags=newline_tags)
'Hello world!'

Credits

The code is extracted from utilities used in several projects, written by Mikhail Korobov.


define hyperiongray

History

0.4.1 (2018-09-25)

Fixed a regression in 0.4.0 release: text was empty when html_text.extract_text is called with a node with text, but without children.

0.4.0 (2018-09-25)

This is a backwards-incompatible release: by default html_text functions now add newlines after elements, if appropriate, to make the extracted text to look more like how it is rendered in a browser.

To turn it off, pass guess_layout=False option to html_text functions.

  • guess_layout option to to make extracted text look more like how it is rendered in browser.

  • Add tests of layout extraction for real webpages.

0.3.0 (2017-10-12)

  • Expose functions that operate on selectors, use .//text() to extract text from selector.

0.2.1 (2017-05-29)

  • Packaging fix (include CHANGES.rst)

0.2.0 (2017-05-29)

  • Fix unwanted joins of words with inline tags: spaces are added for inline tags too, but a heuristic is used to preserve punctuation without extra spaces.

  • Accept parsed html trees.

0.1.1 (2017-01-16)

  • Travis-CI and codecov.io integrations added

0.1.0 (2016-09-27)

  • First release on PyPI.

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

html_text-0.4.1.tar.gz (55.9 kB view details)

Uploaded Source

Built Distribution

html_text-0.4.1-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file html_text-0.4.1.tar.gz.

File metadata

  • Download URL: html_text-0.4.1.tar.gz
  • Upload date:
  • Size: 55.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for html_text-0.4.1.tar.gz
Algorithm Hash digest
SHA256 0876a2ca198ddfe3cc13e5002f4de7462c3669a55707ae3af14c8e7bfd9dfc89
MD5 fb9a8f8e16a34f3bdfe7cfea24aeeab0
BLAKE2b-256 764530cea0ef7060f2e7e6286c325673ffc34cd9458d46e3c76376a8374f9f44

See more details on using hashes here.

File details

Details for the file html_text-0.4.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for html_text-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4549c68e15396d17a3eb68fdbdebcebb6101bca9fb748bdf4c7036436f052b63
MD5 d11f6285407d16405bccc53c2a612adc
BLAKE2b-256 b88255b11f734d5e7982fe4f84e27499c7860eb3acb6c3ed284605de4677c691

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