An open API service indexing awesome lists of open source software.

https://github.com/fmilthaler/htmlparser

Python class to scrap and parse a webpage (using requests, BeautifulSoup4), mainly for converting tables to pandas.DataFrame
https://github.com/fmilthaler/htmlparser

html-parser html-table-parser scraping-websites

Last synced: about 1 year ago
JSON representation

Python class to scrap and parse a webpage (using requests, BeautifulSoup4), mainly for converting tables to pandas.DataFrame

Awesome Lists containing this project

README

          



python


license

# HTMLParser
*HTMLParser* is a class for scrapping and parsing a webpage. Especially useful for converting a table in HTML syntax to a `pandas.DataFrame`.

## Example
Here we scrap a page from Wikipedia, parse it for tables, and convert the first table found into a `pandas.DataFrame`.

```
from htmlparser import HTMLParser
import pandas

# Here we scrap a page from Wikipedia, parse it for tables, and convert the first table found into a `pandas.DataFrame`.
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
hp = HTMLParser(url)
# scrapping the webpage
page = hp.scrap_url()
# extracting only tables from the webpage
element = 'table'
params = {'class': 'wikitable sortable'}
elements = hp.get_page_elements(page, element=element, params=params)
# get a pandas.DataFrame from the (first) html table
df = hp.parse_html_table(elements[0])
print(df.columns.values)
```

This results in the following output (column headers):

```
['Symbol' 'Security' 'SEC filings' 'GICS Sector' 'GICS Sub Industry'
'Headquarters Location' 'Date first added' 'CIK' 'Founded']
```