Git Product home page Git Product logo

htmldate's Introduction

htmldate: find the creation date of HTML pages

image

image

image

image

image

Code

https://github.com/adbar/htmldate

Documentation

https://htmldate.readthedocs.io

Issue tracker

https://github.com/adbar/htmldate/issues

>>> 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'

Contents

Features

htmldate finds the creation date of web pages using a combination of tree traversal, common structural patterns, text-based heuristics and robust date extraction. All the steps needed from web page download to HTML parsing, including scraping and textual analysis are handled. URLs, HTML files or HTML trees are given as input, the library outputs a date string in the desired format.

htmldate provides following ways to date a HTML document:

  1. Markup in header: common patterns are used to identify relevant elements (e.g. link and meta elements) including Open Graph protocol attributes and a large number of CMS idiosyncracies
  2. HTML code: The whole document is then searched for structural markers: abbr/time elements and a series of attributes (e.g. postmetadata)
  3. Bare HTML content: A series of heuristics is run on text and markup:
  1. in fast mode the HTML page is cleaned and precise patterns are targeted
  2. in extensive mode date expressions are collected and the best one is chosen based on a disambiguation algorithm

The module then returns a date if a valid cue could be found in the document, per default the updated date w.r.t. the original publishing statement. The output string defaults to ISO 8601 YMD format.

  • Should be compatible with all common versions of Python 3 (see tests and coverage)
  • Safety belt included, the output is thouroughly verified with respect to its plausibility and adequateness
  • Designed to be computationally efficient and used in production on millions of documents
  • Handles batch processing of a list of URLs

The library currently focuses on texts in written English or German.

Installation

Install from package repository: pip install htmldate

For the latest version (check build status): pip install git+https://github.com/adbar/htmldate.git

With Python

>>> from htmldate import find_date
>>> find_date('http://blog.python.org/2016/12/python-360-is-now-available.html')
'2016-12-23'

The module can resort to a guess based on a complete screning 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)
>>>

Input format

The module expects strings as shown above, it is also possible to use already parsed HTML (i.e. a LXML tree object):

# simple HTML document as string
>>> htmldoc = '<html><body><span class="entry-date">July 12th, 2016</span></body></html>'
>>> find_date(mytree)
'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'

Date format

The output format of the dates found can be set in a format known to Python's datetime module, the default being %Y-%m-%d:

>>> find_date('https://www.gnu.org/licenses/gpl-3.0.en.html', outputformat='%d %B %Y')
'18 November 2016' # may have changed since

Original date

Although the time delta between the original publication and the last modified statement is usually a matter of hours or days at most, it can be useful in some contexts to prioritize the original publication date during extraction:

>>> find_date('https://netzpolitik.org/2016/die-cider-connection-abmahnungen-gegen-nutzer-von-creative-commons-bildern/') # default setting
'2019-06-24'
>>> find_date('https://netzpolitik.org/2016/die-cider-connection-abmahnungen-gegen-nutzer-von-creative-commons-bildern/', original_bool=True) # modified behavior
'2016-06-23'

On the command-line

A basic command-line interface is included:

$ htmldate -u http://blog.python.org/2016/12/python-360-is-now-available.html
'2016-12-23'

For usage instructions see htmldate -h:

The batch mode -i takes one URL per line as input and returns one result per line in tab-separated format:

$ htmldate -i list-of-urls.txt

Additional information

Going further

For more details check the online documentation: htmldate.readthedocs.io

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.

Pull requests are welcome.

Context

This module is part of methods to derive metadata from web documents in order to build text corpora for computational linguistic and NLP analysis, the original problem being that there are web pages for which neither the URL nor the server response provide a reliable way to date the document, i.e. find when it was first published and/or last modified. For more information:

Kudos to...

Contact

See my contact page for details.

htmldate's People

Contributors

adbar avatar derkozmonaut avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.