首页 > 编程语言 >python练习

python练习

时间:2023-05-16 11:24:56浏览次数:31  
标签:count plt python 练习 break year print select

函数图形 1 绘制.py

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,python,练习,break,year,print,select
From: https://www.cnblogs.com/yunbianshangdadun/p/17404382.html

相关文章

  • python练习8
    函数图形2绘制.pyimportnumpyasnpimportmatplotlib.pyplotasplt#定义函数deff1(x):returnx**2deff2(x):returnnp.cos(2*x)deff3(x):returnf1(x)*f2(x)#生成X轴数据x=np.linspace(0,10,500)#绘制函数图形plt.plot(x,f1(x),'b-.',label=......
  • python练习5
    python练习5importmathclassPoint():def__init__(self,x,y):self.x=xself.y=ydef__lt__(self,other):l1=math.sqrt(self.x**2+self.y**2)l2=math.sqrt(other.x**2+other.y**2)returnl1<l2def__le__(sel......
  • python练习4
    python练习4classCexception:def__init__(self,year,month,day):ifCexception.judge(year,month,day):self.year=yearself.month=monthself.day=dayelse:self.year=-1self.mont......
  • python学生管理系统笔记(基础框架)
     1.LoginPage.pyimporttkinterastkfromtkinterimportmessageboxfromdbimportdbfromMainPageimportMainPageclassLoginPage:def__init__(self,master):self.root=masterself.root.geometry('300x180')se......
  • Python多线程并发通用模板
    多线程可以同时处理多个任务,支持并发处理,从而提高系统的并发能力。多线程爬虫的好处主要有提高爬取效率、提高稳定性、节省资源等。总之,多线程爬虫可以提高爬取效率、稳定性和资源利用率,是一种更加高效、可靠的爬虫实现方式。多线程爬虫并行可以提高爬虫的效率,具体实现方法如下:......
  • Python_报错:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Op
    解决:https://blog.csdn.net/Jimmmyking/article/details/126105788作为mac的用户,如果你还没安装Homebrew那真的就太遗憾了,应为其真的很好用,然后安装Homebrew有时候有不是那么简单,会出现很多奇奇怪怪的错误,如下是我本人第一次安装就成功,其重要用的是中科大的brew主体,使用这个只需......
  • Python金融应用编程:衍生品定价和套期保值的随机过程|附代码数据
    全文链接:http://tecdat.cn/?p=5620最近我们被客户要求撰写关于金融应用编程的研究报告,包括一些图形和统计输出。在本文中随机过程对定量融资的许多方面都很有用,包括但不限于衍生品定价,风险管理和投资管理这些应用程序将在本文后面进一步详细讨论。本节介绍了量化融资中使用的一......
  • python 中 pyfaidx 模块统计fasta文件每一条染色体的长度
     001、python版本和pip版本a、python版本[root@PC1pip]#python--versionPython3.11.3 b、pip版本[[email protected]]#pip--versionpip23.1.2from/usr/local/lib/python3.11/site-packages/pip(python3.11) 002、利用pip安装 pyfaidx模块......
  • ERROR: Command errored out with exit status 1: python setup.py egg_info Check th
     001、在利用python2.7环境下利用pip安装pyfaidx模块时报如下错误:ERROR:Commanderroredoutwithexitstatus1:pythonsetup.pyegg_infoCheckthelogsforfullcommandoutput. 002、查看pip版本[root@PC1pip]#pip--versionpip20.3.4from/usr/lib/pyth......
  • 离线安装python模块
    安装环境32位win7电脑+python3.7.5模块来源(1)https://www.lfd.uci.edu/~gohlke/pythonlibs(2)https://pypi.org/安装numpy-1.21.6——直接安装pandas-1.3.5——所需支持:numpy-1.21.6,cython-0.29.30,six-1.16.0,python-dateutil-2.8.2,pytz2023.3matplotlib-2.2.5——所需支持:n......