# !/usr/bin/env python
# -*-coding:utf-8 -*-
import os.path
import bs4,shutil,time
from pandas.core.frame import DataFrame
def get_html_tabledata(htmlpath,tableindex: int = 0):
"""
html file , Get table data
:param htmlpath: html File path
:param tableindex: table Indexes ,int, The default is 0
:return: Dictionary list
"""
with open(htmlpath, 'r+',encoding='UTF-8') as f:
s = f.read()
wb = s.strip().replace('\ufeff', '')
soup = bs4.BeautifulSoup(wb, 'lxml') # analysis html
# Get the data of the specified table
table=soup.findAll("table")[tableindex] # Read the second table
table_rows = table.findAll("tr") # Get the set of rows in the table
# Get the first row of the table as a dictionary keykey
keys = [table_rows[0].findAll(['th', 'td'])[i].getText().strip() for i in range(len(table_rows[0].findAll(['th', 'td']))) ]
tabledata = []
for table_row in table_rows[1:]:
row = table_row.findAll(['th', 'td']) # obtain th/td label
linedata = {keys[i]: row[i].getText().strip() for i in range(len(row))} # Each row of data is returned by field : Key value pair
tabledata.append(linedata)
# print(tabledata)
return tabledata
def html_to_excel(htmlpath,excelpath,tableindex: int = 0):
"""html file , Save the specified table data to excel file """
tabledata = get_html_tabledata(htmlpath,tableindex)
data = DataFrame(tabledata) # Convert dictionary list to table style
# print(data,len(data),len(data.columns)) # Get the number of lines :len(df); Get the number of columns :len(df.columns)
# write in excel
data.to_excel(excelpath, index=False, header=True) # Output as table , Without column number , Output file name
if __name__ == '__main__':
htmlpath = r'C:\Users\yhen\Downloads\2022-06-17T13_51_06+0800.html'
tabledata = get_html_tabledata(htmlpath,1)
tabledata = sorted(tabledata,key=lambda x:x[' keyword '])
print(tabledata)