convert the soup to a pandas dataframe in python

Assuming you have imported pandas and parsed an HTML file with BeautifulSoup into an object called soup, you can convert the contents of a table into a pandas dataframe using the following code:

main.py
import pandas as pd

table = soup.find('table')
df = pd.read_html(str(table))[0]
81 chars
5 lines

In this code, we first find the table tag in the HTML using the find() method of BeautifulSoup. Then str(table) is passed as an argument to the pandas read_html() function to parse the HTML and convert it to a dataframe. The [0] indexer is used to select the first (and hopefully only) table found in the HTML.

related categories

gistlibby LogSnag