Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ Welcome to Html2Data

Description
===========
A simple way to transform a HTML file or URL to structured data. You only need to define the xpath to the element. Optionaly you can define functions to be applied after. You can easily write XPATH using the firebug extension, copy XPATH (I recommend edit the XPATH given by firebug, making it shorter).

A simple way to transform an HTML file or URL to structured data. You only need to define the xpath to the element. Optionally you can define functions to be applied afterwards. You can easily write XPATH using the firebug extension, copy XPATH (I recommend editing the XPATH given by firebug, making it shorter).

Installation
============

::

>> easy_install html2data

Example
=======

Import
------

::

>>> from html2data import HTML2Data

Create instance
---------------

::

>>> html = """<!DOCTYPE html><html lang="en"><head>
Expand All @@ -34,14 +39,16 @@ Create instance
</head>
<body>
<h1><b>Title</b></h1>
<div class="description">This is not a valid HTML
<div class="description">This is not valid HTML
</body>
</html>"""
>>> h2d_instance = HTML2Data(html = html) #You can also create it from a url = url
>>> h2d_instance = HTML2Data(html=html) # You can also create it from a url = url

Using XPATH config
--------------------
One you have the object

Once you have the object

::

>>> config = [
Expand All @@ -50,11 +57,12 @@ One you have the object
{'name': 'description', 'xpath': '//div[@class="description"]/text()'},
]

>>> h2d_instance.parse(config = config)
{'header_title': 'Example Page', 'body_title': 'Title', 'description': 'This is not a valid HTML'}
>>> h2d_instance.parse(config=config)
{'header_title': 'Example Page', 'body_title': 'Title', 'description': 'This is not valid HTML'}

Using CSS SELECTOR config
-------------------------

::

>>> config = [
Expand All @@ -64,11 +72,12 @@ Using CSS SELECTOR config
]

>>> h2d_instance.parse(config = config)
{'header_title': 'Example Page', 'body_title': 'Title', 'description': 'This is not a valid HTML'}
{'header_title': 'Example Page', 'body_title': 'Title', 'description': 'This is not valid HTML'}


Real life example
-----------------

::

import urllib2
Expand All @@ -85,30 +94,32 @@ Real life example
{'name': 'estado', 'css': 'td:nth-child(4)'},
]

html_instance = HTML2Data(html = html)
rows = html_instance.parse_one(css = 'td td tr', multiple = True, text = False)
html_instance = HTML2Data(html=html)
rows = html_instance.parse_one(css='td td tr', multiple=True, text=False)
for row_element in rows[1:]:
row_in_html = HTML2Data(tree = row_element)
print row_in_html.parse(config = config)
row_in_html = HTML2Data(tree=row_element)
print row_in_html.parse(config=config)

You will get something like:

::

{'nombre': 'Reforma Constitucional que restablece obligatoriedad del voto.', 'fecha': '24/11/2011', 'estado': 'En tramitación', 'id': '8062-07'}
..
{'nombre': 'Prohíbe el anatocismo.', 'fecha': '02/11/2011', 'estado': 'En tramitación', 'id': '8007-03'}


Requirement
===========
Requirements
============

* lxml 2.0+
* httplib2

Tests
=====
Requirement
-----------

Requirements
------------

* ludibrio
* nose
Expand All @@ -117,4 +128,3 @@ Run
---

>> nosetests