import bs4
import requests
import re
from bs4 import BeautifulSoup
def getHTMLText(url):
try:
r = requests.get(url,timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def fillUnivList(uList,html):
soup = BeautifulSoup(html,"html.parser")
tempuList=[]
for a_href in soup.find_all('a',href=re.compile('/institution/')):
for tds in a_href.parent.next_siblings:
tempuList.append(tds)
uList.append([a_href.string, tempuList[0].text.replace("\n",""),tempuList[1].text.replace("\n",""),tempuList[2].text.replace("\n",""),tempuList[3].string.replace("\n","")])
tempuList = []
def printUnivList(uList,num):
print("{:^10}\t{:^5}\t{:^30}\t{:^14}\t{:^30}\t{:^14}".format("排名","学校","城市","类型","总分","层次"))
for i in range(num):
u = uList[i]
x=i+1
print("{:^10}\t{:^10}\t{:^10}\t{:^10}\t{:^10}\t{:^10}".format(x,u[0],u[1],u[2],u[3],u[4]))
def main():
uinfo=[]
url ="https://www.shanghairanking.cn/rankings/bcur/2020"
html = getHTMLText(url)
fillUnivList(uinfo,html)
printUnivList(uinfo,20)
main()