adbar/htmldate

GitHub: adbar/htmldate

htmldate 是一个通过启发式算法从网页 HTML 结构和文本内容中精准提取发布日期与更新日期的 Python 工具库。

Stars: 154 | Forks: 33

# Htmldate:查找网页的发布日期 [![Python package](https://img.shields.io/pypi/v/htmldate.svg)](https://pypi.python.org/pypi/htmldate) [![Python versions](https://img.shields.io/pypi/pyversions/htmldate.svg)](https://pypi.python.org/pypi/htmldate) [![Documentation Status](https://readthedocs.org/projects/htmldate/badge/?version=latest)](https://htmldate.readthedocs.org/en/latest/?badge=latest) [![Code Coverage](https://img.shields.io/codecov/c/github/adbar/htmldate.svg)](https://codecov.io/gh/adbar/htmldate) [![Downloads](https://img.shields.io/pypi/dm/htmldate?color=informational)](https://pepy.tech/project/htmldate) [![JOSS article reference DOI: 10.21105/joss.02439](https://img.shields.io/badge/JOSS-10.21105%2Fjoss.02439-brightgreen)](https://doi.org/10.21105/joss.02439)
Htmldate Logo
查找任何网页的**原始和更新后的发布日期**。 通常仅靠 URL 或服务器响应无法做到这一点。 **在命令行或使用 Python 时**,包含了从网页下载到 HTML 解析、抓取和文本分析所需的所有步骤。 该包已在数百万份文档的生产环境中使用,并集成到了[数千个项目](https://github.com/adbar/htmldate/network/dependents)中。 ## 简而言之
Demo as GIF image
### 使用 Python ``` >>> from htmldate import find_date >>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html') '2016-12-23' ``` ### 在命令行中 ``` $ htmldate -u http://blog.python.org/2016/12/python-360-is-now-available.html '2016-12-23' ``` ## 功能 - 灵活的输入:URL、HTML 文件或 HTML 树都可以作为输入 (包括批处理)。 - 可定制的输出:任何日期格式(默认为 [ISO 8601 YMD](https://en.wikipedia.org/wiki/ISO_8601))。 - 检测原始和更新日期。 - 支持多语言。 - 兼容 Python 3.10 及更高版本。 ### 工作原理 Htmldate 通过筛选 HTML 标记,并在必要时筛选文本来运行。它具有以下启发式方法: 1. **头部标记**:使用常见的模式来识别相关 元素(例如 `link` 和 `meta` 元素),包括 [Open Graph 协议](http://ogp.me/)属性。 2. **HTML 代码**:在整个文档中搜索结构标记, 如 `abbr` 或 `time` 元素以及一系列属性(例如 `postmetadata`)。 3. **纯 HTML 内容**:对文本和标记运行启发式算法: - 在 `fast` 模式下,HTML 页面会被清理,并针对 特定的模式。 - 在 `extensive` 模式下,收集所有潜在的日期,并使用 消歧算法确定最佳日期。 最后,验证输出并将其转换为所选的格式。 ## 性能 1000 个包含可识别日期的网页(截至 2026-06-01,在 Python 3.13 上) | Python 包 | 精确率 | 召回率 | 准确率 | F 分数 | 时间 | | -------------- | --------- | ------ | -------- | ------- | ---- | | articleDateExtractor 0.20 | 0.846 | 0.745 | 0.656 | 0.792 | 3x | | date_guesser 2.1.4 | 0.832 | 0.611 | 0.544 | 0.705 | 11x | | goose3 3.1.21 | **0.930** | 0.568 | 0.545 | 0.706 | 14x | | htmldate\[all\] 1.10.0 (fast) | 0.924 | 0.927 | 0.861 | 0.925 | **1x** | | htmldate\[all\] 1.10.0 (extensive) | 0.908 | **0.993** | **0.903** | **0.949** | 1.8x | | newspaper4k 0.9.5 | 0.912 | 0.728 | 0.680 | 0.810 | 2.5x | | news-please 1.6.16 | 0.845 | 0.777 | 0.680 | 0.810 | 29x | 有关完整的结果和解释,请参阅[评估 页面](https://htmldate.readthedocs.io/en/latest/evaluation.html)。 ## 安装说明 Htmldate 已在 Linux、macOS 和 Windows 系统上过测试,它兼容 Python 3.10 及更高版本。它特别可以通过 PyPI 包存储库使用 `pip`(适用时使用 `pip3`)进行安装: - `pip install htmldate` - (可选)`pip install htmldate[speed]` 支持 Python 3.6 和 3.7 的最后版本是 `htmldate==1.8.1`;对于 Python 3.8 和 3.9,请使用 `1.9.x` 系列。 ## 文档 有关安装、Python 和 CLI 使用的更多详细信息,**请参阅 文档**: [htmldate.readthedocs.io](https://htmldate.readthedocs.io/) ## 许可证 该包在 [Apache 2.0 许可证](https://www.apache.org/licenses/LICENSE-2.0.html)下分发。 v1.8.0 之前的版本采用 GPLv3+ 许可证。 ## 背景与贡献 该项目最初启动是为了在柏林-勃兰登堡科学与人文学院(DWDS 和 ZDL 部门)创建用于研究目的的文本数据库, 该项目仍在继续维护,但其未来的发展 取决于社区的支持。 如有任何疑问、合作意向或反馈,请通过软件代码库或[联系页面](https://adrien.barbaresi.eu/) 联系我们。 [![JOSS article reference DOI: 10.21105/joss.02439](https://img.shields.io/badge/JOSS-10.21105%2Fjoss.02439-brightgreen)](https://doi.org/10.21105/joss.02439) [![Zenodo archive DOI: 10.5281/zenodo.3459599](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.3459599-blue)](https://doi.org/10.5281/zenodo.3459599) ``` @article{barbaresi-2020-htmldate, title = {{htmldate: A Python package to extract publication dates from web pages}}, author = "Barbaresi, Adrien", journal = "Journal of Open Source Software", volume = 5, number = 51, pages = 2439, url = {https://doi.org/10.21105/joss.02439}, publisher = {The Open Journal}, year = 2020, } ``` - Barbaresi, A. \"[htmldate: A Python package to extract publication dates from web pages](https://doi.org/10.21105/joss.02439)\", Journal of Open Source Software, 5(51), 2439, 2020. DOI: 10.21105/joss.02439 - Barbaresi, A. \"[Generic Web Content Extraction with Open-Source Software](https://hal.archives-ouvertes.fr/hal-02447264/document)\", Proceedings of KONVENS 2019, Kaleidoscope Abstracts, 2019. - Barbaresi, A. \"[Efficient construction of metadata-enhanced web corpora](https://hal.archives-ouvertes.fr/hal-01371704v2/document)\", Proceedings of the [10th Web as Corpus Workshop (WAC-X)](https://www.sigwac.org.uk/wiki/WAC-X), 2016. ## 致谢 感谢以下软件库: - [lxml](http://lxml.de/), [dateparser](https://github.com/scrapinghub/dateparser) - 一些模式派生自 [python-goose](https://github.com/grangier/python-goose), [metascraper](https://github.com/ianstormtaylor/metascraper), [newspaper](https://github.com/codelucas/newspaper) 和 [articleDateExtractor](https://github.com/Webhose/article-date-extractor) 库。该模块显著扩展了它们的覆盖范围和鲁棒性。
标签:IPv6支持, Python, 数据提取, 文本分析, 文档结构分析, 无后门, 网页解析, 逆向工具