Skip to main content

A simple way to transform a HTML file or URL to structured data.

Project description

A simple way to transform a HTML file or URL to structured data. You only need to define the xpath to the element, and a function wich is going to handle the element. You can easily write XPATH using the firebug extension, copy XPATH (I recommend edit the XPATH given by firebug, making it shorter). For example:

>>> ## start the console
>>> from html2data import html2data
>>> html = """<!DOCTYPE html><html lang="en"><head>
        <meta charset="utf-8" />
        <title>Example Page</title>
        <link rel="stylesheet" href="css/main.css" type="text/css" />
    </head>
    <body>
        <h1><b>Title</b></h1>
        <div class="description">This is not a valid HTML</body>
</html>"""
>>> config = {
        'map': [
            ['header_title', u'//head/title/text()', self.getOne],
            ['body_title', u'//h1/b/text()', self.getOne],
            ['description', u'//div[@class="description"]/text()', self.getOne],
        ]
    }
>>> handler = html2data()
>>> received_obj = handler.load(html = html, config=config)
>>> print received_obj
{'header_title': 'Example Page', 'body_title': 'Title', 'description': 'This is not a valid HTML'}
To use it you will need:
  • lxml 2.0+

  • httplib2

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

html2data-0.3.tar.gz (2.4 kB view hashes)

Uploaded Source

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