Skip to content

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

License

Notifications You must be signed in to change notification settings

fmilthaler/HTMLParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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']

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages