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

以个人成绩网页页面为例:
Python读取本地html文件,获取其中表格内容_第1张图片
右键查看源代码:
Python读取本地html文件,获取其中表格内容_第2张图片
右键另存为单独的html文件,然后代码读取并处理

import re

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

table = re.findall(r'', 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'(.*?)', 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'(.*?)', nowtable, re.S)
print("选修课信息为:\n",td1)
print("选修课信息第一个值为:\n",td1[0])

结果:
Python读取本地html文件,获取其中表格内容_第3张图片
如果想要计算GPA,字符转换为对应的数值进行计算就行了

你可能感兴趣的:(Python,python,html,正则表达式)