本文翻译整理自:https://github.com/adbar/htmldate
一、关于 Htmldate
Htmldate 是一款用于提取网页原始发布日期和更新日期的工具。支持通过命令行或Python调用,集成了从网页下载到HTML解析、内容抓取和文本分析的全流程。该工具已在数百万文档的生产环境中验证,并被多个库集成。
相关链接资源
- github : https://github.com/adbar/htmldate
- 官网:https://pypi.python.org/pypi/htmldate
- 官方文档:https://htmldate.readthedocs.io/
- Demo/在线试用:https://htmldate.readthedocs.io/en/latest/evaluation.html
- JOSS论文:https://doi.org/10.21105/joss.02439
- Zenodo存档:https://doi.org/10.5281/zenodo.3459599
- 作者主页:https://adrien.barbaresi.eu/
- License : Apache 2.0
关键功能特性
- 支持URL、HTML文件或HTML树结构输入(含批量处理)
- 可自定义日期输出格式(默认ISO 8601标准)
- 同时检测原始发布日期和更新日期
- 多语言支持
- 兼容Python 3.8及以上版本
二、核心功能演示
1、Python调用示例
>>> from htmldate import find_date
>>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html')
'2016-12-23'
2、命令行调用示例
$ htmldate -u http://blog.python.org/2016/12/python-360-is-now-available.html
'2016-12-23'
三、工作原理
Htmldate通过分析HTML标记和文本内容实现日期检测,采用以下启发式方法:
1、头部标记分析:识别常见元素(如link
和meta
标签)及Open Graph协议属性
2、HTML结构分析:搜索文档中的abbr
、time
等结构化元素及特定属性(如postmetadata
)
3、内容分析:
fast
模式:清理HTML后匹配精确日期模式extensive
模式:收集所有潜在日期并通过消歧算法确定最佳结果
最终输出经校验后转换为指定格式。
四、性能对比
基于1000个含可识别日期网页的测试(2023-11-13/Python 3.10环境)
Python包 | 精确率 | 召回率 | 准确率 | F值 | 耗时倍数 |
---|---|---|---|---|---|
articleDateExtractor 0.20 | 0.803 | 0.734 | 0.622 | 0.767 | 5x |
htmldate[all] 1.6.0 (fast) | 0.883 | 0.924 | 0.823 | 0.903 | 1x |
htmldate[all] 1.6.0 (extensive) | 0.870 | 0.993 | 0.865 | 0.928 | 1.7x |
完整测试结果见:https://htmldate.readthedocs.io/en/latest/evaluation.html
五、安装指南
支持Linux/macOS/Windows系统,需Python 3.8+环境:
pip install htmldate
# 可选性能优化版
pip install htmldate[speed]
注:最后支持Python 3.6/3.7的版本为htmldate==1.8.1
六、其它
1、技术文献
- Barbaresi, A. “htmldate: A Python package to extract publication dates from web pages”, Journal of Open Source Software, 5(51), 2439, 2020. DOI: 10.21105/joss.02439
- 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.
2、致谢
特别感谢以下开源项目:
lxml
、dateparser
日期解析库- 部分模式参考自
python-goose
、metascraper
等工具
伊织 xAI 2025-04-19(六)