栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas爬取表格

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas爬取表格

系列文章目录

文章目录
  • 系列文章目录
  • 下载写入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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878899.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号