trafilatura: Web scraping tool for text discovery and retrieval¶
- Code
- Documentation
- Issue tracker
![Demo as GIF image](_images/trafilatura-demo.gif)
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:¶
Most efficient open-source library in ScrapingHub’s article extraction benchmark as well as in another independant evaluation on the same data.
Best overall tool according to Gaël Lejeune & Adrien Barbaresi, Bien choisir son outil d’extraction de contenu à partir du Web (2020, PDF, French).
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¶
Software¶
![Software ecosystem](_images/software-ecosystem.png)
Trafilatura: Italian word for wire drawing.