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

python 提取出所有学生的序号,姓名,成绩(简单易懂,代码可以直接运行)

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

python 提取出所有学生的序号,姓名,成绩(简单易懂,代码可以直接运行)

python 提取出所有学生的序号 姓名 成绩(简单易懂 代码可以直接运行)(正则表达式提取)

数据如下

 tbody 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 1 /span 张婷婷 /span /td td class opr-toplist-right 92 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 2 /span 王华 /span /td td class opr-toplist-right 91 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 3 /span 张岚 /span /td td class opr-toplist-right 90 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 4 /span 孙鸿峰 /span /td td class opr-toplist-right 90 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 5 /span 周海栋 /span /td td class opr-toplist-right 89 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 6 /span 武静 /span /td td class opr-toplist-right 88 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 /tbody 

我一共是用来两种方法解开了这个题 一个将数据存入了字典 一个将数据存入了元组中 第一种方法麻烦一点 但是可以用于正则表达式爬取网络中的信息 点个 吧 代码如下

import re
import requests
#提取所有学生的序号 姓名 成绩
str tbody 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 1 /span 张婷婷 /span /td td class opr-toplist-right 92 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 2 /span 王华 /span /td td class opr-toplist-right 91 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-index-hot1 c-gap-icon-right-small 3 /span 张岚 /span /td td class opr-toplist-right 90 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 4 /span 孙鸿峰 /span /td td class opr-toplist-right 90 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 5 /span 周海栋 /span /td td class opr-toplist-right 89 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 tr td span span class c-index c-gap-icon-right-small 6 /span 武静 /span /td td class opr-toplist-right 88 i class opr-toplist-st c-icon c-icon-down /i /td /tr 
 /tbody 
#爬取数据利用则这表达式 一步一步缩小范围
#首先建立一个大列表用来存储小字典的信息
student []
tr_list re.findall(r tr (.*?) /tr ,str,re.S)#先将每一个tr一次提出来
#print(tr_list)
for tr in tr_list:
 #num re.search(r c-gap-icon-right-small (.*)? /span ,tr,re.S).group(1),出错会多提取一些元素1 /span 张婷婷
 num re.search(r c-gap-icon-right-small (d)? /span ,tr,re.S).group(1)
 name re.search(r /span (.*?) /span , tr, re.S).group(1)
 grade re.search(r td class opr-toplist-right (.*?) i class opr-toplist-st c-icon c-icon-down , tr, re.S).group(1)
 n n 1
 #建立字典存储信息
 item {}#建立一个空字典
 item[ num ] num
 item[ name ] name
 item[ grade ] grade
 print(item.values())
 student.append(item)
 # num re.search(r span class c-index c-index-hot1 c-gap-icon-right-small (.*?) /span , tr, re.S)
# print(n)
print(student)
#方法二 正则表达式
slt re.findall(r c-gap-icon-right-small (d ) /span (w ) /span /td td class opr-toplist-right (d ) i class opr-toplist-st c-icon c-icon-down ,str,re.S)
print(slt)

点个 吧

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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