首页 > 其他分享 >5.30

5.30

时间:2024-06-19 15:21:47浏览次数:9  
标签:count plt rank 5.30 year print select

完成 8-1 【Python0025】中国大学排名数据分析与可视化 分数 10 作者 doublebest 单位 石家庄铁道大学

【题目描述】以软科中国最好大学排名为分析对象,基于requests库和bs4库编写爬虫程序,对2015年至2019年间的中国大学排名数据进行爬取:

(1)按照排名先后顺序输出不同年份的前10位大学信息,并要求对输出结果的排版进行优化;

(2)结合matplotlib库,对2015-2019年间前10位大学的排名信息进行可视化展示。

(3附加)编写一个查询程序,根据从键盘输入的大学名称和年份,输出该大学相应的排名信息。如果所爬取的数据中不包含该大学或该年份信息,则输出相应的提示信息,并让用户选择重新输入还是结束查询;

# coding=gb2312

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

标签:count,plt,rank,5.30,year,print,select
From: https://www.cnblogs.com/jais/p/18256339

相关文章

  • 5.30博客
    周三:python和工程数学python学习:importrequestsfrombs4importBeautifulSoupasbsimportpandasaspdfrommatplotlibimportpyplotaspltdefget_rank(url):count=0 rank=\[\] headers={    "user-agent":"Mozilla/5.0(WindowsNT10......
  • 5.30
    今日总结学习时间很久请看代码packagecom.app.chapter03;importstaticcom.app.chapter03.R.*;importandroid.os.Bundle;importandroid.widget.TextView;importandroidx.activity.EdgeToEdge;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.graph......
  • 5.30安卓开发日记47
    ① 、实现的功能为对错题信息的增删改查Ⅰ、主页面 Ⅱ、错题录入 Ⅲ、错题查询(可根据题目搜索) Ⅳ、错题修改 Ⅴ、错题删除(选中行后右键选择删除) Ⅵ、显示详细信息 ......
  • 5.30
    import'package:flutter/material.dart';WidgetbuildCustomDrawer({requiredStringaccountEmail,requiredStringaccountName,requiredStringcurrentAccountPictureAsset,requiredStringdrawerBackgroundAsset,requiredList<DrawerItem>......
  • 2024.5.30
    8-2【Python0026】图书评论数据分析与可视化分数10全屏浏览作者 doublebest单位 石家庄铁道大学【题目描述】豆瓣图书评论数据爬取。以《平凡的世界》、《都挺好》等为分析对象,编写程序爬取豆瓣读书上针对该图书的短评信息,要求:(1)对前3页短评信息进......
  • 2024.05.30
    function[s,phis,k,G,E]=golds(phi,a,b,delta,epsilon)%输入:phi是目标函数,a,b是搜索区间的两个端点delta,epsilon分别是自变量和函数值的容许误差9%输出:s,phis分别是近似极小点和极小值,G是nx4矩阵。其第k行分别是a,p,q,b的第k次迭代值[ak,pk,qk,bk],E=[ds,dphi],分别是s和phis的误......
  • 5.30
    完成python作业  8-1【Python0025】中国大学排名数据分析与可视化【题目描述】以软科中国最好大学排名为分析对象,基于requests库和bs4库编写爬虫程序,对2015年至2019年间的中国大学排名数据进行爬取:(1)按照排名先后顺序输出不同年份的前10位大学信息,并要求对输出结果的排......
  • 5.30
    做完了安卓端的政策查询系统页面跳转时数据传递这儿想到一个不一样的方法页面跳转的主函数这儿初始化viewModel避免了每次使用viewModel的初始化,将他作为一个参数传递给各个页面@ComposablefunAppNavHost(rootNavController:NavHostController=rememberNavContro......
  • 5.30
    近二十年来,我们一直致力于加速计算的研究。CUDA技术增强了CPU的功能,将那些特殊处理器能更高效完成的任务卸载并加速。事实上,由于CPU性能扩展的放缓甚至停滞,加速计算的优势愈发显著。我预测,每个处理密集型的应用都将实现加速,且不久的将来,每个数据中心都将实现全面加速。”“数百万......
  • 5.30学习总结
    CSS3弹性盒子(FlexBox)弹性盒子是CSS3的一种新的布局模式。CSS3弹性盒(FlexibleBox或flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、......