Skip to main content

A python library for extracting data from html table

Project description

# HTML Table Extractor
_HTML Table Extractor is a python library that uses [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) extract data from complicated and messy html table_

## Important links
* Repository: https://github.com/yuanxu-li/html-table-extractor
* Issues: https://github.com/yuanxu-li/html-table-extractor/issues

## Installation

```bash
pip install 'beautifulsoup4==4.5.3'
pip install html-table-extractor
```

## Usage

### Example 1 - Simple

<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>

```python
from html_table_extractor.extractor import Extractor
table_doc = """
<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>
"""
extractor = Extractor(table_doc)
extractor.parse()
extractor.return_list()
```
It will print out:
```python
[[u'1', u'2'], [u'3', u'4']]
```

### Example 2 - Transformer

<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>

```python
from html_table_extractor.extractor import Extractor
table_doc = """
<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>
"""
extractor = Extractor(table_doc, transformer=int)
extractor.parse()
extractor.return_list()
```
It will print out:
```python
[[1, 2], [3, 4]]
```

### Example 3 - Pass BS4 Tag

<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>

```python
from html_table_extractor.extractor import Extractor
from bs4 import BeautifulSoup
table_doc = """
<html><table id='wanted'><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table><table id='unwanted'><tr><td>not wanted</td></tr></table></html>
"""
soup = BeautifulSoup(table_doc, 'html.parser')
extractor = Extractor(soup, id_='wanted')
extractor.parse()
extractor.return_list()
```
It will print out:
```python
[[u'1', u'2'], [u'3', u'4']]
```

### Example 4 - Complex

<table>
<tr>
<td rowspan=2>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td colspan=2>4</td>
</tr>
<tr>
<td colspan=3>5</td>
</tr>
</table>

```python
from html_table_extractor.extractor import Extractor
table_doc = """
<table>
<tr>
<td rowspan=2>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td colspan=2>4</td>
</tr>
<tr>
<td colspan=3>5</td>
</tr>
</table>
"""
extractor = Extractor(table_doc)
extractor.parse()
extractor.return_list()
```
It will print out:
```python
[[u'1', u'2', u'3'], [u'1', u'4', u'4'], [u'5', u'5', u'5']]
```

### Example 5 - Conflicted

<table>
<tr>
<td rowspan=2>1</td>
<td>2</td>
<td rowspan=3>3</td>
</tr>
<tr>
<td colspan=2>4</td>
</tr>
<tr>
<td colspan=2>5</td>
</tr>
</table>

```python
from html_table_extractor.extractor import Extractor
table_doc = """
<table>
<tr>
<td rowspan=2>1</td>
<td>2</td>
<td rowspan=3>3</td>
</tr>
<tr>
<td colspan=2>4</td>
</tr>
<tr>
<td colspan=2>5</td>
</tr>
</table>
"""
extractor = Extractor(table_doc)
extractor.parse()
extractor.return_list()
```
It will print out:
```python
[[u'1', u'2', u'3'], [u'1', u'4', u'3'], [u'5', u'5', u'3']]
```

### Example 6 - Write to file

<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>

```python
from html_table_extractor.extractor import Extractor
table_doc = """
<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>
"""
extractor = Extractor(table_doc).parse()
extractor.write_to_csv(path='.')
```
It will write to a given path and create a new csv file called `output.csv`:
```
1,2
3,4

```

## Team

* [@yuanxu-li](https://github.com/yuanxu-li)

## Errors/ Bugs

If something is not working correctly, or if you have any suggestion on improvements, [report it here](https://github.com/yuanxu-li/table-extractor/issues)

## Copyright

Copyright (c) 2017 Justin Li. Released under the [MIT License](https://github.com/yuanxu-li/html-table-extractor/blob/master/README.md)

Third-party copyright in this distribution is noted where applicable.


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

html-table-extractor-1.2.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

html_table_extractor-1.2.0-py2-none-any.whl (5.5 kB view details)

Uploaded Python 2

File details

Details for the file html-table-extractor-1.2.0.tar.gz.

File metadata

  • Download URL: html-table-extractor-1.2.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14

File hashes

Hashes for html-table-extractor-1.2.0.tar.gz
Algorithm Hash digest
SHA256 5bb1e427dfd8ddddc51ec4f3e3dcf9eba09b25b44f3723f150a153c339958266
MD5 351e30fca6094834cff81b9e523691a7
BLAKE2b-256 02376bea43e3400de9cb8c9238bd923a365f9b9e42b86fb96725651f4cf8ea1b

See more details on using hashes here.

File details

Details for the file html_table_extractor-1.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for html_table_extractor-1.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 d8b9a6510d7e0b7b0d363036b20dc7a25f11f5ea2db32e033acbc2f439c24070
MD5 255587bfe3a6ac73e6323639b9cd2e76
BLAKE2b-256 acb6e483a56af55c749bcb8f28b2d9eb24b08ce0bac4201a26dca081192ddaf8

See more details on using hashes here.

Supported by

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