当前位置: 移动技术网 > IT编程>脚本编程>Python > Python读取本地html文件,获取其中表格内容

Python读取本地html文件,获取其中表格内容

2020年07月18日  | 移动技术网IT编程  | 我要评论

以个人成绩网页页面为例:
在这里插入图片描述
右键查看源代码:
在这里插入图片描述
右键另存为单独的html文件,然后代码读取并处理

import re

f = open("GP.html","r",encoding='utf-8')
html = f.read()

table = re.findall(r'<table(.*?)</table>', html, re.S)#查找html中table之间的内容
nowtable = table[0]#前两个表格为成绩信息
nowtable = nowtable.replace('\t','')#将空格换行等去除
nowtable = nowtable.replace('\n','')
nowtable = nowtable.replace(' ','')
nowtable = nowtable.replace('&nbsp','')
td0 = re.findall(r'<tdclass="center">(.*?)</td>', nowtable, re.S)#成绩想关的信息都在tdclass="center"td之间
print("主修课程信息为:\n",td0)
nowtable = table[1]
nowtable = nowtable.replace('\t','')
nowtable = nowtable.replace('\n','')
nowtable = nowtable.replace(' ','')
nowtable = nowtable.replace('&nbsp','')
td1 = re.findall(r'<tdclass="center">(.*?)</td>', nowtable, re.S)
print("选修课信息为:\n",td1)
print("选修课信息第一个值为:\n",td1[0])

结果:
在这里插入图片描述
如果想要计算GPA,字符转换为对应的数值进行计算就行了

本文地址:https://blog.csdn.net/qq_37813206/article/details/107380221

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网