```python
import requests
from bs4 import BeautifulSoup
allUniv=[]
def getHTMLText(url):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding='utf-8'
return r.text
except:
return "网页链接有误"
def fillUnivList(soup):
data=soup.find_all("tr")
for tr in data:
ltd=tr.find_all("td")
if len(ltd)==0:
continue
singUniv=[]
for td in ltd:
singUniv.append(td.string)
allUniv.append(singUniv)
def printUnivList(num):
print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))
for i in range(num):
u=allUniv[i]
print("{:^4}{:^10}{:^5}{:^8}{:^10}".format(u[0],u[1],u[2],u[3],u[6]))
def main(num):
url="https://www.shanghairanking.cn/rankings/bcur/2022"
html=getHTMLText(url)
soup=BeautifulSoup(html,"html.parser")
fillUnivList(soup)
printUnivList(num)
main(10)
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/075236115966181.jpg "#left")