trafilatura: Web scraping tool for text discovery and retrieval

Python package Python versions Code Coverage Downloads

Code

https://github.com/adbar/trafilatura

Documentation

https://trafilatura.readthedocs.io/

Issue tracker

https://github.com/adbar/trafilatura/issues


Demo as GIF image

Description

Trafilatura is a Python package and command-line tool which seamlessly downloads, parses, and scrapes web page data: it can extract metadata, main body text and comments while preserving parts of the text formatting and page structure. The output can be converted to different formats.

Distinguishing between a whole page and the page’s essential parts can help to alleviate many quality problems related to web text processing, by dealing with the noise caused by recurring elements (headers and footers, ads, links/blogroll, etc.).

The extractor aims to be precise enough in order not to miss texts or to discard valid documents. In addition, it must be robust, but also reasonably fast. With these objectives in mind, Trafilatura is designed to run in production on millions of web documents. It is based on lxml as well as readability and jusText as fallback.

Features

  • Seamless and parallel online/offline processing:
    • Download and conversion utilities included

    • URLs, HTML files or parsed HTML trees as input

  • Robust and efficient extraction:
    • Main text and/or comments

    • Structural elements preserved: paragraphs, titles, lists, quotes, code, line breaks, in-line text formatting

    • Extraction of metadata (title, author, date, site name, categories and tags)

  • Several output formats supported:
    • Text (minimal formatting or Markdown)

    • CSV (with metadata, tab-separated values)

    • JSON (with metadata)

    • XML (for metadata and structure) and TEI-XML

  • Link discovery and URL lists:
    • Focused crawling and politeness rules

    • Support for sitemaps (TXT, XML) and feeds (ATOM, JSON, RSS)

    • Efficient and polite processing of URL queues

    • Blacklisting

  • Optional language detection on extracted content

Evaluation and alternatives

The extraction focuses on the main content: usually the part displayed centrally, without left or right bars, header or footer, but including potential titles and (optionally) comments. These tasks are also known as web scraping, boilerplate removal, DOM-based content extraction, main content identification, or web page cleaning.

For reproducible results see the evaluation page and the evaluation script.

External evaluations:

In a nutshell

Primary installation method is with a Python package manager: pip install trafilatura. See installation documentation.

With Python:

>>> import trafilatura
>>> downloaded = trafilatura.fetch_url('https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/')
>>> trafilatura.extract(downloaded)
# outputs main content and comments as plain text ...

On the command-line:

$ trafilatura -u "https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/"
# outputs main content and comments as plain text ...

For more information please refer to usage documentation and tutorials.

License

Trafilatura is distributed under the GNU General Public License v3.0. If you wish to redistribute this library but feel bounded by the license conditions please try interacting at arms length, multi-licensing with compatible licenses, or contacting me.

See also GPL and free software licensing: What’s in it for business?

Going further

Trafilatura: Italian word for wire drawing.

Corresponding posts on Bits of Language (blog).

Roadmap

For a comprehensive list see issues page.

  • [-] Duplicate detection at sentence, paragraph and document level using a least recently used (LRU) cache

  • [-] URL lists and document management

  • [-] Configuration and extraction parameters

  • [-] Graphical user interface

  • [ ] Interaction with web archives (notably WARC format)

  • [ ] Integration of natural language processing tools

Contributing

Contributions are welcome! Please also feel free to file issues on the dedicated page.

Many thanks to the contributors who submitted features and bugfixes!

Context

Author

This effort is part of methods to derive information from web documents in order to build text databases for research (chiefly linguistic analysis and natural language processing). Extracting and pre-processing web texts to the exacting standards of scientific research presents a substantial challenge for those who conduct such research. Web corpus construction involves numerous design decisions, and this software package can help facilitate text data collection and enhance corpus quality.

https://zenodo.org/badge/DOI/10.5281/zenodo.3460969.svg

You can contact me via my contact page or GitHub.

To have a chat about the software feel free to book a 15 min slot for video-conferencing.

Software

Software ecosystem

Trafilatura: Italian word for wire drawing.

Known uses of the software.

Further documentation

Indices and tables