Skip to main content

Wikipedia HTML Dump Parsing

Project description

mwparserfromhtml

mwparserfromhtml is a Python library for parsing and mining metadata from the Enterprise HTML Dumps that has been recently made available by the Wikimedia Enterprise. The 6 most updated Enterprise HTML dumps can be accessed from this location. The aim of this library is to provide an interface to work with these HTML dumps and extract the most relevant features from an article.

Besides using the HTML dumps, users can also use the Wikipedia API to obtain the HTML of a particular article from their title and parse the HTML string with this library.

Motivation

When rendering contents, MediaWiki converts wikitext to HTML, allowing for the expansion of macros to include more material. The HTML version of a Wikipedia page generally has more information than the original source wikitext. So, it's reasonable that anyone who wants to analyze Wikipedia's content as it appears to its readers would prefer to work with HTML rather than wikitext. Traditionally, only the wikitext version has been available in the XML-dumps. Now, with the introduction of the Enterprise HTML dumps in 2021, anyone can now easily access and use HTML dumps (and they should).

However, parsing HTML to extract the necessary information is not a simple process. An inconspicuous user may know how to work with HTMLs but they might not be used to the specific format of the dump files. Also the wikitext translated to HTMLs by the MediaWiki API have many different edge-cases and requires heavy investigation of the documentation to get a grasp of the structure. Identifying the features from this HTML is no trivial task! Because of all these hassles, it is likely that individuals would continue working with wikitext as there are already excellent ready-to-use parsers for it (such as mwparserfromhell). Therefore, we wanted to write a Python library that can efficiently parse the HTML-code of an article from the Wikimedia Enterprise dumps to extract relevant elements such as text, links, templates, etc. This will hopefully lower the technical barriers to work with the HTML-dumps and empower researchers and others to take advantage of this beneficial resource.

Features

  • Iterate over large tarballs of HTML dumps without extracting them to memory (memory efficient, but not subscriptable unless converted to a list)
  • Extract major article metadata like Category, Templates, Wikilinks, External Links, Media, References etc. with their respective type and status information
  • Easily extract the content of an article from the HTML dump and customizing the level of detail
  • Generate summary statistics for the articles in the dump

Installation

You can install mwparserfromhtml with pip:

   $ pip install mwparserfromhtml

Basic Usage

  • Import the dump module from the library and load the dump:
    from mwparserfromhtml import HTMLDump
    html_file_path = "TARGZ_FILE_PATH"
    html_dump = HTMLDump(html_file_path, max_article=150)
  • Iterate over the articles in the dump:
    for article in html_dump:
        print(article.title)
  • Extract the plain text of an article from the dump, i.e. remove anything that is not text (e.g. a link is replaced by its anchor text):
    for article in html_dump:
        print(article.get_plaintext( skip_categories=False, skip_transclusion=False, skip_headers=False))
  • Extract Templates, Categories, Wikilinks, External Links, Media, References etc. from the dump:
    for article in html_dump:
        print(article.get_templates())
        print(article.get_categories())
        print(article.get_wikilinks())
        print(article.get_external_links())
        print(article.get_media(skip_images=True, skip_video=False, skip_audio=False))
        print(article.get_references())
  • Parse HTML string of a Wikipedia article (in a file FILE.html) and extract features (such as templates)
    from mwparserfromhtml import Article
    html_string = "".join(open("FILE.html", "r").readlines())
    article = Article(html_string)
    print(article.get_templates())

Project Information

Acknowledgements

This project was started as part of an Outreachy internship from May--August 2022. This project has benefited greatly from the work of Earwig (mwparserfromhell) and Slavina Stefanova (mwsql).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwparserfromhtml-0.0.3.tar.gz (51.4 kB view details)

Uploaded Source

Built Distribution

mwparserfromhtml-0.0.3-py3-none-any.whl (53.0 kB view details)

Uploaded Python 3

File details

Details for the file mwparserfromhtml-0.0.3.tar.gz.

File metadata

  • Download URL: mwparserfromhtml-0.0.3.tar.gz
  • Upload date:
  • Size: 51.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.64.0 importlib-metadata/4.8.2 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.7

File hashes

Hashes for mwparserfromhtml-0.0.3.tar.gz
Algorithm Hash digest
SHA256 977bd1fa220489eead7cd0c7e60bfcd990094942f892713930ce4a4980b3f859
MD5 aeb4aba40cee7684f07295684b7fd98c
BLAKE2b-256 31a5eec57c0a9e27924fe4d385f195e0796bb9b722d125b143fdbc37f308937a

See more details on using hashes here.

File details

Details for the file mwparserfromhtml-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mwparserfromhtml-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 53.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.64.0 importlib-metadata/4.8.2 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.7

File hashes

Hashes for mwparserfromhtml-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 70096523c45310233f479b492131e7af1a2cfd323a85c4d2be929e169bce146f
MD5 f12e39edd28564b9bef920e5b6006907
BLAKE2b-256 83ad9f406527295afe923cec2c7cd763d1d5379e46a97caaa0724bfe41b3c159

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page