Skip to main content

A HT/XML web scraping tool

Project description

https://travis-ci.org/datalib/libextract.svg?branch=master
    ___ __              __                  __
   / (_) /_  ___  _  __/ /__________ ______/ /_
  / / / __ \/ _ \| |/_/ __/ ___/ __ `/ ___/ __/
 / / / /_/ /  __/>  </ /_/ /  / /_/ / /__/ /_
/_/_/_.___/\___/_/|_|\__/_/   \__,_/\___/\__/

Libextract is a statistics-enabled extraction library that works on HTML and XML documents, written in Python and originating from eatiht.

Overview

Libextract provides two extractors out-of-the-box: api.articles and api.tabular

libextract.api.articles(document, encoding=’utf-8’, count=5)

Given an html document, and optionally the encoding and the number of predictions (count) to return (in descending rank), articles returns a list of HTML-nodes likely containing the articles of text of a given website.

The extraction algorithm is based of text length. Refer to rodricios.github.io/eatiht for an in-depth explanation.

libextract.api.tabular(document, encoding=’utf-8’, count=5)

Given an html document, and optionally the encoding, and the number of predictions (count) to return (in descending rank) tabular returns a list of HTML nodes likely containing “tabular” data (ie. table, and table-like elements).

Installation

pip install libextract

Usage

Extracting text-nodes from a wikipedia page:

from requests import get
from libextract.api import articles

r = get('http://en.wikipedia.org/wiki/Information_extraction')
textnodes = articles(r.content)

Libextract uses Python’s de facto HT/XML processing library, lxml.

The predictions returned by both api.articles and api.tabular are lxml HtmlElement objects (along with the associated metric used to rank each prediction).

Therefore, you can access lxml’s methods for post-processing.

>> print(textnodes[0][0].text_content())
Information extraction (IE) is the task of automatically extracting structured information...

Tabular-data extraction is just as easy.

from libextract.api import tabular

height_data = get("http://en.wikipedia.org/wiki/Human_height")
tabs = tabular(height_data.content)

To convert HT/XML element to python dict (and, you know, use it with Pandas and stuff):

>>> from libextract import clean
>>> clean.to_dict(tabs[0])
{'Entity': ['Monaco',
  'Macau',
  'Japan',
  'Singapore',
  'San Marino',
  ...}

Dependencies

lxml
statscounter

Disclaimer

This project is still in its infancy; and advice and suggestions as to what this library could and should be would be greatly appreciated

:)

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

libextract-0.0.11.zip (46.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page