- 系列文章目录
- 下载写入excel
import pandas as pd
url="http://qhggzyjy.gov.cn/haib/jyxx/001002/001002002/20220512/f828d85d-f0b0-42f5-b77f-e4d915d313c0.html"
tables = pd.read_html(url)
print("table数量:",len(tables))
#读取第一个表
tables[0]下载写入excel
import pandas as pd
url="http://www.stats.gov.cn/tjsj/tjgb/jjpcgb/qgjpgb/201407/t20140731_590163.html"
tables = pd.read_html(url)
print("table数量:",len(tables))
writer = pd.ExcelWriter(r"网页表.xlsx",engine = "xlsxwriter")
i=1
for table in tables[1:]:
df=table.fillna('类型')#替换
if len(df)<=2:
continue
name=df.iloc[0, 0].replace('u3000','')#表sheet名
df.columns = list(df.iloc[1])#选择第二行作为表
df = df.drop([0,1], axis=0)#删除第一行
#写入
print(name)
df.to_excel(writer,sheet_name = name,index=False)
i+=1
writer.save()
writer.close()



