README 未给出命令行用法示例,需查阅官方文档的 CLI 页面;语言检测与速度优化属于可选附加组件,需另行启用
trafilatura
adbarApache-2.0活跃维护从网页抓取正文和元数据的 Python 库与命令行工具,支持爬取、解析并导出为 CSV、JSON、Markdown 等格式,适合构建语料库和 RAG 数据。
Python & Command-line tool to gather text and metadata on the Web: Crawling, scraping, extraction, output as CSV, JSON, HTML, MD, TXT, XML
pip install trafilaturaREADME 未说明,未提及 GPU 或显存要求
从网页抓取正文与元数据的 Python 库和命令行工具,把原始 HTML 变成结构化数据
从网页抓取正文和元数据的 Python 库与命令行工具,支持爬取、解析并导出为 CSV、JSON、Markdown 等格式,适合构建语料库和 RAG 数据。
核心能力
- 用规则提取器抽取正文,jusText 与 readability-lxml 兜底
- 提取标题、作者、日期、站点名、分类标签等元数据
- 支持 sitemap 与 ATOM/JSON/RSS 订阅源发现链接并去重
- 并行处理在线 URL 与本地 HTML 文件
- 输出 TXT、Markdown、CSV、JSON、HTML、XML 及 XML-TEI
容易踩的坑
- v2.3.1trafilatura-2.3.1最新正式版2026-10-06 · 2026-10-06 19:31 UTC
- Fewer duplicated segments in the output and a new test series by @adbar (#955)
- Do not repeat image paragraphs or the text after inline code by @MohammadHijjawi97 (#946, #952)
- Keep brackets inside inline code in link text unescaped by @Sriram-PR (#954)
- v2.3.0trafilatura-2.3.02026-10-02 · 2026-10-02 16:53 UTC
查看更新说明
Major changes:
- Downloads and decompression overhaul by @adbar (#921)
- Simplified document handling code and better correctness by @adbar (#938)
- Simpler and faster deduplication, scoped caches accepted by @adbar (#919)
Fixes:
- Better text extraction: forms, link farms, inline code, infinite scroll, boilerplate widgets, inline links, nested lists and text after blocks by @cvl01, @ebarkhordar, @bobtheskull-flameeyes, @emecii, @SulimanAbdulrazzaq and @r0h1tb (#884, #898, #901, #911, #916, #927, #929, #937, #940)
- Better Markdown output: spacing, bold spans, CommonMark escaping and LaTeX math by @cvl01, @scottwulf, @ebarkhordar and @jonathanavis96 (#899, #902, #915, #924)
- Preserve requested images in fallbacks by @vvsotnikov (#904)
- Better metadata: word boundaries, JSON-LD types, OpenGraph, license links, list values, canonical URLs and TXT/Markdown header by @adbar, @xblwh, @mfurkanakinci and @MohammadHijjawi97 (#918, #932, #934, #936, #941, #942, #947)
- More robust downloads and link discovery: relative URLs, zstd frames, sitemap namespaces, Atom links and JSON feeds by @ebarkhordar, @audi0417, @be-student and @mfurkanakinci (#909, #920, #926, #933, #935)
Maintenance:
- Refine and restructure the docs by @adbar (#908)
- Link URL discovery tools from extraction guidance by @workstonedai-collab (#944)
- v2.2.0trafilatura-2.2.02026-07-31 · 2026-07-31 16:07 UTC
查看更新说明
Major changes:
- Revamped text recovery and extraction sequence: better recall overall and better extraction on forums by @adbar (#881)
- More targeted boilerplate pruning to reduce over-extraction by @adbar (#887)
- More robust tests and better processing for nested elements by @adbar (#866)
- Better handling of text in sub-elements by @adbar (#869)
Fixes:
- Improve title extraction by @novucs (#848)
- Restore text inside nobr-tags by @kod-kristoff (#826)
- Keep space after inline formatting in Markdown list items by @cycsmail (#865)
- Keep list items whose only content is a link by @cycsmail (#867)
- Keep Yoast FAQ block question headers by @cycsmail (#868)
- Address output formatting bugs by @adbar (#870)
- Further improve subelem processing, mostly math, code and Markdown by @adbar (#872)
- More accurate table processing by @adbar (#874)
- Quote metadata values to keep the Markdown header valid YAML by @gaoflow (#875)
- Escape angle brackets in Markdown link destinations by @gaoflow (#877)
- Match image extensions case-insensitively in is_image_file by @lntutor (#888)
- Keep sup and sub boundaries in Markdown output by @igorsaevets (#891)
Maintenance:
- Review function arguments and add warnings by @adbar (#876)
- Code hardening and bug roundup by @adbar (#863)
- Add tests and a page on testing by @cycsmail (#873)
- Add example for adapting element lists in Python by @cycsmail (#878)
- Select ruff rules explicitly so CI is deterministic by @igorsaevets (#892)
- Run the lint step under bash so failures are not discarded on Windows by @igorsaevets (#893)
Full Changelog: https://github.com/adbar/trafilatura/compare/v2.1.0...v2.2.0
- v2.1.0trafilatura-2.1.02026-06-07 · 2026-06-07 17:44 UTC
查看更新说明
Major changes:
- Dependencies updated, lxml in particular (with minimal changes in the code)
- Faster XPath performance using XSLT extensions by @Honesty-of-the-Cavernous-Tissue (#793)
- More deprecation warnings
- More robust code
Fixes:
- Fix node pruning raising an AttributeError in certain cases by @PLPeeters (#761)
- Refine img src url and fix table extraction bugs by @unsleepy22 (#762)
- Fix eager doctype regex matching by @UVMvmfee (#764)
- Add extract_with_metadata method and ut by @unsleepy22 (#765)
- More robust table extraction by @unsleepy22 (#767)
- Detect code in pre-tag using indicators and fix code formatting issues by @steineggerroland (#776)
- Fix tokenizer not stripping "Ideographic Full Stop" by @reinoldus (#783)
- Improve spacing in item, cell and code blocks by @unsleepy22 (#772)
- Changed code to handle potential empty string by @seanswyi (#790)
- Optimize PUNCT_TBL creation by @deedy5 (#791)
- Replace
Nones in code blocks by @crackcomm in (#797) - Propagate no_fallback setting to fast argument by @unsleepy22 (#800)
- Fix bug #807 by @arcombe012 (#808)
- Added support to meta name image by @felipehertzer (#809)
- Preserve images when links are included by @rmarchei (#841)
- Clean fencedframe element during extraction by @eyupcanakman (#847)
- Add all Justext languages known to the detector with @cikay (#810)
- Safer lxml element init by @adbar (#856)
- Preserve links in tables when --links is enabled by @rmarchei (#858)
Maintenance:
- Update docs by @adbar (#773)
- Update README: fix /dependents links by @vbarbaresi (#785)
- Update CI pipeline by @adbar (#801)
- Update setup, fix code and tests by @adbar (#853)
- Review consistency of code, tests and docs by @adbar (#855)
- Add tests to prevent metadata bugs by @adbar (#857)
- Tests: check consistency by @adbar (#859)
- Modernize code base by @adbar (#860)
- Format the code with ruff by @adbar (#861)
- v2.0.0trafilatura-2.0.02024-12-03 · 2024-12-03 15:23 UTC
查看更新说明
Breaking changes:
- Python 3.6 and 3.7 deprecated (#709)
bare_extraction():- now returns an instance of the
Documentclass by default as_dictdeprecation warning → use.as_dict()method on return value (#730)
- now returns an instance of the
bare_extraction()andextract():no_fallbackdeprecation warning → usefastinstead (#730)- downloads: remove
decodeargument infetch_url()→ usefetch_responseinstead (#724) - deprecated graphical user interface now removed (#713)
- extraction: move
max_tree_sizeparameter tosettings.cfg(#742) - use type hinting (#721, #723, #748)
- see Python and CLI deprecations in the docs
Fixes:
- set
options.sourcebefore raising error on empty doc tree by @dmoklaf (#707) - robust encoding in
options.source(#717) - more robust mapping for conversion to HTML (#721)
- CLI downloads: use all information in settings file (#734)
- downloads: cleaner urllib3 code (#736)
- refine table markdown output by @unsleepy22 (#752)
- extraction fix: images in text nodes by @unsleepy22 (#757)
Metadata:
- more robust URL extraction (#710)
Command-line interface:
- CLI: print URLs early for feeds and sitemaps with
--listwith @gremid (#744) - CLI: add 126 exit code for high error ratio (#747)
Maintenance:
- remove already deprecated functions and args (#716)
- add type hints (#723, #728)
- setup: use
pyproject.tomlfile (#715) - simplify code (#708, #709, #727)
- better debug messages in
main_extractor(#714) - evaluation: review data, update packages, add magic_html (#731)
- setup: explicit exports through
__all__(#740) - tests: extend coverage (#753)
Documentation:
- fix link in
docs/index.htmlby @nzw0301 (#711) - remove docs from published packages (#743)
- update docs (#745)
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Trafilatura: Discover Web Content, Extract Text and Metadata
>>> from trafilatura import fetch_url, extract
>>> downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")
>>> extract(downloaded)
'Erin Spiceland is a software engineer and ...'
>>> extract(downloaded, output_format="json", with_metadata=True)
'{"title": "Leader spotlight: Erin Spiceland", "author": "Jessica Rudder", "text": "Erin Spiceland is a software engineer and ..."}'
Introduction
Trafilatura is a comprehensive Python package and command-line tool designed to gather text from the Web and turn raw HTML into structured, meaningful data. It includes all necessary discovery and text processing components to perform web crawling, downloads, scraping, and extraction of main texts, metadata and comments. It aims at staying handy and modular: no database is required, the output can be converted to commonly used formats.
Going from raw HTML to essential parts, it focuses on the actual content, avoids noise caused by recurring elements (headers, footers, boilerplate), and makes sense of the data and metadata. The extractor strikes a balance between limiting noise (precision) and including all valid parts (recall). It is robust and reasonably fast.
Trafilatura is widely used across thousands of projects, including by HuggingFace, IBM, Microsoft Research, NVIDIA, the Allen Institute for AI, Stanford, and the Internet Archive.
Features
-
Advanced web crawling and text discovery:
- Support for sitemaps (TXT, XML) and feeds (ATOM, JSON, RSS)
- Smart crawling and URL management (filtering and deduplication)
-
Parallel processing of online and offline input:
- Live URLs, efficient and polite processing of download queues
- Previously downloaded HTML files and parsed HTML trees
-
Robust and configurable extraction of key elements:
- Main text (own rule-based extractor with jusText and readability-lxml as fallbacks)
- Metadata (title, author, date, site name, categories and tags)
- Formatting and structure: paragraphs, titles, lists, quotes, code, line breaks, in-line text formatting
- Optional elements: comments, links, images, tables
- Optional add-ons: language detection, speed optimizations
-
Multiple output formats:
- TXT and Markdown
- CSV
- JSON
- HTML, XML and XML-TEI
Evaluation
Trafilatura consistently outperforms other open-source libraries in text extraction benchmarks. For more information see the benchmark section and the evaluation readme to run the evaluation with the latest data and packages.
Other evaluations
- Most efficient open-source library in ScrapingHub's article extraction benchmark
- Best overall tool according to Bien choisir son outil d'extraction de contenu à partir du Web (Lejeune & Barbaresi 2020)
- Best single tool by ROUGE-LSum Mean F1 Page Scores in An Empirical Comparison of Web Content Extraction Algorithms (Bevendorff et al. 2023)
Documentation
Getting started with Trafilatura is straightforward. For more information and detailed guides, visit Trafilatura's documentation:
- Installation
- Usage: On the command-line, With Python, With R
- Core Python functions
- Interactive Python Notebook: Trafilatura Overview (in the repository)
- Tutorials and use cases
See the video tutorials playlist (multiple languages).
License
This package is distributed under the Apache 2.0 license.
Versions prior to v1.8.0 are under GPLv3+ license.
Contributing
Contributions of all kinds are welcome. Visit the Contributing page for more information. Bug reports can be filed on the dedicated issue page.
Many thanks to the contributors who extended the docs or submitted bug reports, features and bugfixes!
Support
If you value this software or depend on it for your product, consider sponsoring it and contributing to its codebase. Your support on GitHub or ko-fi.com will help maintain and enhance this package.
Context
This work started as a PhD project at the crossroads of linguistics and NLP. This expertise has been instrumental in shaping Trafilatura over the years. Initially launched to create text databases for research purposes at the Berlin-Brandenburg Academy of Sciences (DWDS and ZDL units), this package continues to be maintained and its future depends on community support.
Trafilatura is an Italian word for wire drawing symbolizing the refinement and conversion process. It is also the way shapes of pasta are formed.
Author
Reach out via the software repository or the contact page for inquiries, collaborations, or feedback. See also social networks for the latest updates.
- Barbaresi, A. Trafilatura: A Web Scraping Library and Command-Line Tool for Text Discovery and Extraction, Proceedings of ACL/IJCNLP 2021: System Demonstrations, 2021, p. 122-131.
- Barbaresi, A. "Generic Web Content Extraction with Open-Source Software", Proceedings of KONVENS 2019, Kaleidoscope Abstracts, 2019.
- Barbaresi, A. "Efficient construction of metadata-enhanced web corpora", Proceedings of the 10th Web as Corpus Workshop (WAC-X), 2016.
Citing Trafilatura
Trafilatura is widely used in the academic domain, chiefly for data acquisition. Here is how to cite it:
@inproceedings{barbaresi-2021-trafilatura,
title = {{Trafilatura: A Web Scraping Library and Command-Line Tool for Text Discovery and Extraction}},
author = "Barbaresi, Adrien",
booktitle = "Proceedings of the Joint Conference of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing: System Demonstrations",
pages = "122--131",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2021.acl-demo.15",
year = 2021,
}
Software ecosystem
Jointly developed plugins and additional packages also contribute to the field of web data extraction and analysis:
Corresponding posts can be found on Bits of Language.