read table from html


Thu Feb 02 2023 21:17:32 GMT+0000 (UTC)

Saved by @quaie #python

# variation of

table = soup.find_all('table')[2]		# we receive a list of tables
table_body = table.find('tbody')

data = [] 
rows = table_body.find_all('tr')
for row in rows:
    cols = row.find_all(['td', 'th'])	# consider rows AND headers 
    cols = [ele for ele in cols]		# get html tags, not .text 
    data.append([ele for ele in cols if ele])

# directly via pandas
import pandas as pd
dfs = pd.read_html(content[1]['body'])				#list of
dfs[0].columns = ['key', 'value']					# set column names
dfs[0]#[dfs[0].key == 'Status'].iloc[0]['value']	# read cell value

beautiful soup --> read html table to list of rows (can parse html tags)