htmldate: find the publication date of web pages¶
- Issue tracker
Find original and updated publication dates of any web page. From the command-line or within Python, all the steps needed from web page download to HTML parsing, scraping, and text analysis are included.
In a nutshell, with Python:
>>> from htmldate import find_date >>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html') '2016-12-23' >>> find_date('https://netzpolitik.org/2016/die-cider-connection-abmahnungen-gegen-nutzer-von-creative-commons-bildern/', original_date=True) '2016-06-23'
On the command-line:
$ htmldate -u http://blog.python.org/2016/12/python-360-is-now-available.html '2016-12-23'
Compatible with all recent versions of Python (see above)
Multilingual, robust and efficient (used in production on millions of documents)
URLs, HTML files, or HTML trees are given as input (includes batch processing)
Output as string in any date format (defaults to ISO 8601 YMD)
Detection of both original and updated dates
htmldate finds original and updated publication dates of web pages using heuristics on HTML code and linguistic patterns. It provides following ways to date a HTML document:
Markup in header: Common patterns are used to identify relevant elements (e.g.
metaelements) including Open Graph protocol attributes and a large number of CMS idiosyncrasies
HTML code: The whole document is then searched for structural markers:
timeelements as well as a series of attributes (e.g.
Bare HTML content: A series of heuristics is run on text and markup:
fastmode the HTML page is cleaned and precise patterns are targeted
extensivemode all potential dates are collected and a disambiguation algorithm determines the best one
The output is thouroughly verified in terms of plausibility and adequateness and the library outputs a date string, corresponding to either the last update or the original publishing statement (the default), in the desired format (defaults to ISO 8601 YMD format).
Markup-based extraction is multilingual by nature, text-based refinements for better coverage currently support German, English and Turkish.
This Python package is tested on Linux, macOS and Windows systems, it is compatible with Python 3.6 upwards. It is available on the package repository PyPI and can notably be installed with
$ pip install htmldate # pip3 install on systems where both Python 2 and 3 are installed $ pip install --upgrade htmldate # to make sure you have the latest version $ pip install git+https://github.com/adbar/htmldate.git # latest available code (see build status above)
Additional libraries can be installed to enhance efficiency:
ciso8601 (for speed). They may not work on all platforms and have thus been singled out although installation is recommended:
$ pip install htmldate[speed] # install with additional functionality
You can also install or update the packages separately, htmldate will detect which ones are present on your system and opt for the best available combination.
For infos on dependency management of Python packages see this discussion thread.
All the functions of the module are currently bundled in htmldate.
In case the web page features easily readable metadata in the header, the extraction is straightforward. A more advanced analysis of the document structure is sometimes needed:
>>> from htmldate import find_date >>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html') '# DEBUG analyzing: <h2 class="date-header"><span>Friday, December 23, 2016</span></h2>' '# DEBUG result: 2016-12-23' '2016-12-23'
htmldate can resort to a guess based on a complete screening of the document (
extensive_search parameter) which can be deactivated:
>>> find_date('https://creativecommons.org/about/') '2017-08-11' # has been updated since >>> find_date('https://creativecommons.org/about/', extensive_search=False) >>>
Already parsed HTML (that is a LXML tree object):
# simple HTML document as string >>> htmldoc = '<html><body><span class="entry-date">July 12th, 2016</span></body></html>' >>> find_date(htmldoc) '2016-07-12' # parsed LXML tree >>> from lxml import html >>> mytree = html.fromstring('<html><body><span class="entry-date">July 12th, 2016</span></body></html>') >>> find_date(mytree) '2016-07-12'
Change the output to a format known to Python’s
datetime module, the default being
>>> find_date('https://www.gnu.org/licenses/gpl-3.0.en.html', outputformat='%d %B %Y') '18 November 2016' # may have changed since >>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html', outputformat='%Y-%m-%dT%H:%M:%S%z') '2016-12-23T05:11:00-0500'
Although the time delta between original publication and “last modified” info is usually a matter of hours or days, it can be useful to prioritize the original publication date:
>>> find_date('https://netzpolitik.org/2016/die-cider-connection-abmahnungen-gegen-nutzer-von-creative-commons-bildern/', original_date=True) # modified behavior '2016-06-23'
For more information see options page.
On the command-line¶
A command-line interface is included:
$ htmldate -u http://blog.python.org/2016/12/python-360-is-now-available.html '2016-12-23' $ wget -qO- "http://blog.python.org/2016/12/python-360-is-now-available.html" | htmldate '2016-12-23'
For usage instructions see
$ htmldate --help htmldate [-h] [-f] [-i INPUTFILE] [--original] [-min MINDATE] [-max MAXDATE] [-u URL] [-v] [--version] optional arguments: -h, --help show this help message and exit -f, --fast fast mode: disable extensive search -i INPUTFILE, --inputfile INPUTFILE name of input file for batch processing (similar to wget -i) --original original date prioritized -min MINDATE, --mindate MINDATE earliest acceptable date (YYYY-MM-DD) -max MAXDATE, --maxdate MAXDATE latest acceptable date (YYYY-MM-DD) -u URL, --URL URL custom URL download -v, --verbose increase output verbosity --version show version information and exit
The batch mode
-i takes one URL per line as input and returns one result per line in tab-separated format:
$ htmldate --fast -i list-of-urls.txt
htmldate 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.
Contributions are welcome!
Kudos to the following software libraries:
The granularity may not always match the desired output format. If only information about the year could be found and the chosen date format requires to output a month and a day, the result is ‘padded’ to be located at the middle of the year, in that case the 1st of January.
Besides, there are pages for which no date can be found, ever:
>>> r = requests.get('https://example.com') >>> htmldate.find_date(r.text) >>>
If the date is nowhere to be found, it might be worth considering carbon dating the web page, however this is computationally expensive. In addition, datefinder features pattern-based date extraction for texts written in English.
A series of webpages triggering different structural and content patterns is included for testing purposes:
$ pytest tests/unit_tests.py