Skip to main content

Extract text from HTML

Project description

PyPI Version Supported Python Versions Build Status Coverage report

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 users;

  • html_text normalizes whitespace, but in a way smarter than .xpath('normalize-space()), adding spaces around inline elements (which are often used as block elements in html markup), and trying to avoid adding extra spaces for punctuation;

  • html-text can add newlines (e.g. after headers or paragraphs), so that the output text looks more like how it is rendered in browsers.

Install

Install with pip:

pip install html-text

The package depends on lxml, so you might need to install 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!'

Passed html is first cleaned from invisible non-text content such as styles, and then text is extracted.

You can also pass an 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!'

If you want, you can handle cleaning manually; use lower-level html_text.etree_to_text in this case:

>>> import html_text
>>> tree = html_text.parse_html('<h1>Hello<style>.foo{}</style>!</h1>')
>>> cleaned_tree = html_text.cleaner.clean_html(tree)
>>> html_text.etree_to_text(cleaned_tree)
'Hello!'

parsel.Selector objects are also supported; you can define a parsel.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'

NB parsel.Selector objects are not cleaned automatically, you need to call html_text.cleaned_selector first.

Main functions and objects:

  • html_text.extract_text accepts html and returns extracted text.

  • html_text.etree_to_text accepts parsed lxml Element and returns extracted text; it is a lower-level function, cleaning is not handled here.

  • html_text.cleaner is an lxml.html.clean.Cleaner instance which can be used with html_text.etree_to_text; its options are tuned for speed and text extraction quality.

  • 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!'

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.

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.7.1.tar.gz (54.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

html_text-0.7.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: html_text-0.7.1.tar.gz
  • Upload date:
  • Size: 54.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for html_text-0.7.1.tar.gz
Algorithm Hash digest
SHA256 87fd194310a9f54be32c7b18a70180dfa72a6b4d01cca35ac813c4d2b2b2ed8b
MD5 19ea9c4fcc070ba9614ef5de319e87c6
BLAKE2b-256 675fc8d32dfdb3b03802b4c45555aef07d868eb7561252d6a70459657a1f41c3

See more details on using hashes here.

File details

Details for the file html_text-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: html_text-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for html_text-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 acf13aa1412e559f84f4617fba419720cb8732861736fe07a8dfdafcbdbb8cad
MD5 a86279896d01cbd0d0628fb75eae3b7e
BLAKE2b-256 01da694ee4570fb1d9d06a00f1493a248531d347be48a2503e9dd79c595de992

See more details on using hashes here.

Supported by

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