爬取最好大学排名

import requests
from bs4 import BeautifulSoup
import bs4

url = ‘http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html

ret = requests.get(url=url)

修改字符编码

ret.encoding = ret.apparent_encoding
data = ret.text

将网页文本以‘html.parser’做格式化操作

soup = BeautifulSoup(data, ‘html.parser’)

获取所有的‘tbody’标签的子标签

tr_list = soup.find(‘tbody’).children
ulist = []
for tr in tr_list:
# 判断tr是否是一个标签对象
if isinstance(tr, bs4.element.Tag):
# 拿到tr中所有的td
tds = tr(‘td’)
ulist.append([tds[0].string, tds[1].string,
tds[2].string, tds[3].string])

def print_ulist(ulist, pro):
print(“中国2019{}地区最好大学排名”.format(pro))
tplt = “{0:10}\t{1:{4}10}\t{2:10}\t{3:10}”
print(tplt.format(‘排名’,‘学校排名’,‘地区’,‘评分’,chr(12288)))
if pro == ‘浙江’:
pass
for i in range(len(ulist)):
u = ulist[i]
if u[2] == ‘浙江’:
print(tplt.format(u[0],u[1],u[2],u[3],chr(12288)))

print_ulist(ulist, ‘浙江’)

使用Python爬取中国大学排名有多种实现方式,以下是不同场景下的示例方法: ### 爬取保存到Excel 可以编写Python爬虫代码来爬取中国大学排名保存到Excel中。有示例代码整体框架清晰,可直接使用,能帮助小白学习爬虫知识 [^1]。 ### 爬取保存到CSV 以下是爬取中国大学排名保存到CSV文件的代码示例: ```python def printUnivList(ulist, num): file_name = "大学排行.csv" with open(file_name, 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(["排名", "大学名称", "省市", "类型", "总分"]) for i in range(num): u = ulist[i] writer.writerow(u) print(f"排名:{u[0]}\t大学名称:{u[1]}\t省市:{u[2]}\t类型:{u[3]}\t总分:{u[4]}") ``` 这段代码将爬取到的大学排名信息按指定格式保存到CSV文件中,在控制台打印出部分信息 [^2]。 ### 爬取进行大屏可视化 使用`pyecharts`库可以对爬取的全国大学排名数据进行大屏可视化展示,以地图形式呈现各地区高校数量。示例代码如下: ```python from pyecharts.charts import Map import pandas as pd from pyecharts import options as opts from pyecharts.globals import ThemeType datafile = r'D:/Demo/school.xlsx' data = pd.read_excel(datafile) map = ( Map(init_opts=opts.InitOpts(theme=ThemeType.CHALK)) .add("本科", [list(z) for z in zip(data['城市'].values.tolist(), data['本科'].values.tolist())]) .add("专科", [list(z) for z in zip(data['城市'].values.tolist(), data['专科'].values.tolist())]) .set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=150), title_opts=opts.TitleOpts(title="各地区高校数量")) .render(path='map.html') ) ``` 该代码从Excel文件中读取数据,然后创建一个地图可视化,展示各地区本科和专科高校的数量 [^3]。 ### 爬取存入数据库及设计查询系统界面 在爬虫实训中,可使用Python爬取大学排名存入数据库,还能设计查询系统界面。示例代码展示了界面以及按钮的设计与显示: ```python import tkinter as tk from tkinter import Button root = tk.Tk() root.resizable(False, False) root.minsize(600, 750) root.maxsize(600, 750) root.title('大学排名查询系统') root.config(width=300) root.config(height=300) def all_university_button(root, href_name): button1 = Button(root, text=href_name[0], font=("微软雅黑 -20"), command=University_One) button1.place(x=150, y=50, height=40, width=200) button2 = Button(root, text=href_name[1], font=("微软雅黑 -20"), command=University_Two) button2.place(x=150, y=125, height=40, width=200) button3 = Button(root, text=href_name[2], font=("微软雅黑 -20"), command=University_Three) button3.place(x=150, y=200, height=40, width=200) button4 = Button(root, text=href_name[3], font=("微软雅黑 -20"), command=University_Four) button4.place(x=150, y=275, height=40, width=200) button5 = Button(root, text=href_name[4], font=("微软雅黑 -20"), command=University_Five) button5.place(x=150, y=350, height=40, width=200) ``` 上述代码创建了一个简单的查询系统界面,包含多个按钮,可用于不同的查询操作 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值