Skip to main content

Downloads web pages, scrapes main text and comments while preserving some structure, and converts to TXT, CSV, XML & TEI-XML

Project description

Python package Python versions Documentation Status Travis build status Code Coverage

Demo as GIF image

Description

Trafilatura can seamlessly download, parse and convert web documents: it scrapes the main body text while preserving part of the text formatting and page structure and converts to TXT, CSV, XML & TEI-XML.

Distinguishing between whole page and essential parts can help to alleviate many quality problems related to web texts as it can help with the noise consisting of recurring elements (headers and footers, ads, links/blogroll).

It has to be precise enough not to miss texts or discard valid documents, robust but also reasonably fast. It is designed to run in production on millions of documents.

Features

  • Seamless download and extraction: URLs, HTML files or parsed HTML trees as input

  • Focus on main text and/or comments

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

  • Extraction of metadata (currently title and date, more to come)

  • Output in plain text (minimal formatting), CSV (with metadata, tab-separated values) or XML format (for metadata and structure)

  • Computationally efficient (relies on lxml)

  • Robust extraction and generic readability and jusText algorithms used as fallback

  • Optional language detection on the extracted content

Evaluation and alternatives

For experimental results see the evaluation page and evaluation script. To reproduce the tests just clone the repository, install all necessary packages and run the evaluation script with the data provided in the tests directory.

300 documents, 869 text and 878 boilerplate segments (2020-03-19)

Python Package

Precision

Recall

Accuracy

F-Score

Time

raw HTML

0.519

0.885

0.535

0.654

0

baseline (text markup)

0.726

0.776

0.742

0.750

1.14

html2text 2020.1.16

0.499

0.787

0.501

0.611

11.00

inscriptis 1.0 (html to txt)

0.521

0.962

0.541

0.676

2.47

justext 2.2.0 (German stoplist)

0.849

0.529

0.719

0.652

6.37

newspaper3k 0.2.8

0.923

0.591

0.772

0.721

14.80

goose3 3.1.6

0.957

0.640

0.807

0.767

21.54

boilerpy3 1.0.2 (article mode)

0.841

0.734

0.799

0.784

5.65

dragnet 2.0.4

0.909

0.722

0.825

0.804

3.64

readability-lxml 0.7.1

0.928

0.743

0.844

0.826

6.59

news-please 1.4.25

0.926

0.747

0.844

0.827

70.81

trafilatura 0.3.1 (rule-based)

0.901

0.831

0.871

0.865

5.43

trafilatura 0.3.1 (+ justext)

0.897

0.868

0.884

0.882

6.97

trafilatura 0.4

0.914

0.869

0.894

0.891

4.87

trafilatura 0.4 (+ fallback)

0.925

0.904

0.916

0.914

9.94

Installation

Chiefly with the Python package manager pip: pip install --upgrade trafilatura.

For more details see installation documentation.

Usage

With Python or on the command-line.

In a nutshell, 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 the usage documentation.

License

trafilatura is distributed under the GNU General Public License v3.0

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

Going further

Online documentation: trafilatura.readthedocs.io

Trafilatura: Italian word for wire drawing.

Roadmap

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

  • [-] XML output compatible with the recommendations of the Text Encoding Initiative

  • [X] Metadata integration

  • [-] Language detection on the extracted content

  • [-] Preservation of in-line text formatting (bold, italic, etc.)

  • [ ] Configuration and extraction parameters

Contributing

Contributions are welcome!

Feel free to file bug reports on the issues page.

Thanks to these contributors who submitted features and bugfixes:

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). A significant challenge resides in the ability to extract and pre-process web texts to meet scientific expectations: Web corpus construction involves numerous design decisions, and this software packages can help facilitate 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.

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

trafilatura-0.4.tar.gz (2.4 MB view details)

Uploaded Source

Built Distribution

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

trafilatura-0.4-py3-none-any.whl (139.6 kB view details)

Uploaded Python 3

File details

Details for the file trafilatura-0.4.tar.gz.

File metadata

  • Download URL: trafilatura-0.4.tar.gz
  • Upload date:
  • Size: 2.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9

File hashes

Hashes for trafilatura-0.4.tar.gz
Algorithm Hash digest
SHA256 9e126d47766eec4a7f510cbb25635f97aa38ae968ee8cbf7c97a95bea6801282
MD5 c2a4a1d2df6e459daf3258ff2e1d96f4
BLAKE2b-256 f5232949b2d7369aa3e666ae6045a33577d33d03192ee8cba22349c69af16046

See more details on using hashes here.

File details

Details for the file trafilatura-0.4-py3-none-any.whl.

File metadata

  • Download URL: trafilatura-0.4-py3-none-any.whl
  • Upload date:
  • Size: 139.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9

File hashes

Hashes for trafilatura-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 13424d1c7eba373ae5ec7c1a574f5f37352c0a0327bfce1e847a6cf288889704
MD5 a96f41bce6d557941a1e3fcdab034f49
BLAKE2b-256 dbe35b01df7d4d7ed8630bc86daf79ee6da9f916fa9107a262f312eb3fce705f

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