程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python: get HTML table data, HTML table save Excel

編輯:Python
# !/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)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved