中国大学排名数据分析与可视化

import requests

from bs4 import BeautifulSoup as bs

import pandas as pd

from matplotlib import pyplot as plt

def get_rank(url):

count = 0

rank = []

headers = {

"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.3"

}

resp = requests.get(url, headers=headers).content.decode()

soup = bs(resp, "lxml")

univname = soup.find_all('a', class_="name-cn")

for i in univname:

if count != 10:

university = i.text.replace(" ", "")

score = soup.select("#content-box > div.rk-table-box > table > tbody > tr:nth-child({}) > td:nth-child(5)"

.format(count + 1))[0].text.strip()

rank.append([university, score])

else:

break

count += 1

return rank

total = []

u_year = 2015

for i in range(15, 20):

url = "https://www.shanghairanking.cn/rankings/bcur/20{}11".format(i)

print(url)

title = ['学校名称', '总分']

df = pd.DataFrame(get_rank(url), columns=title)

total.append(df)

for i in total:

plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签

x = list(i["学校名称"])[::-1]

y = list(i["总分"])[::-1]

# 1.创建画布

plt.figure(figsize=(20, 8), dpi=100)

# 2.绘制图像

plt.plot(x, y, label="大学排名")

# 2.2 添加网格显示

plt.grid(True, linestyle="--", alpha=0.5)

# 2.3 添加描述信息

plt.xlabel("大学名称")

plt.ylabel("总分")

plt.title(str(u_year) + "年软科中国最好大学排名Top10", fontsize=20)

# 2.5 添加图例

plt.legend(loc="best")

# 3.图像显示

plt.savefig(str(u_year)+".png")

plt.show()

u_year += 1

while True:

info = input("请输入要查询的大学名称和年份:")

count = 0

university, year = info.split()

year = int(year)

judge = 2019 - year

tmp = total[::-1]

if 4 >= judge >= 0:

name = list(total[judge - 1]["学校名称"])

for j in name:

if university == j:

print(university + "在{0}年排名第{1}".format(year, count + 1))

break

count += 1

if count ==10:

print("很抱歉,没有该学校的排名记录!!!")

print("请选择以下选项:")

print(" 1.继续查询")

print(" 2.结束查询")

select = int(input(""))

if select == 1:

continue

elif select == 2:

break

else:

break

else:

print("很抱歉,没有该年份的排名记录!!!")

print("请选择以下选项:")

print(" 1.继续查询")

print(" 2.结束查询")

select = int(input(""))

if select == 1:

continue

elif select == 2:

break

![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547260-2e213eb1-a590-4a18-8d42-1f9974998fa8.png)![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547306-8be8b597-85c2-4364-b035-560d6dffd053.png)![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547325-3f7eb655-4028-4103-9aba-87b007cec6a9.png)![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547341-0381087c-7a0d-49af-9084-13bbb224e330.png)![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547352-8904dac6-aa04-49ed-9986-9eb7784d6c84.png)![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-6-5/1717547669-1595abfa-9708-47f8-b395-453ae4284dde.png)

posted @ 2024-06-05 08:41  混沌武士丞  阅读(3)  评论(0编辑  收藏  举报