Python读取本地html文件,获取其中表格内容
程序员文章站
2022-03-28 22:25:59
python读取本地html文件并进行简单的处理来获取文字...
以个人成绩网页页面为例:
右键查看源代码:
右键另存为单独的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(' ','')
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(' ','')
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