from bs4 import BeautifulSoup
import requests
allUnivList = []
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = 'utf-8'
return r.text
except:
return " "
def findUnivList(soup):
data=soup.find_all('tr')
for tr in data:
ltd = tr.find_all('td')
if len(ltd) == 0:
continue
singleUniv = []
for td in ltd:
singleUniv.append(td.string)
allUnivList.append(singleUniv)
def printAllUniv(num):
print('{1:^2}{2:{0}^10}{3:{0}^5}{4:{0}^10}{5:{0}^8}'.format(chr(12288),'排名', '学校名称', '省市', '学习类型', '总分'))
for i in range(num):
u = allUnivList[i]
print('{1:^3}{2:{0}^10}{3:{0}^5}{4:{0}^10}{5:{0}^8}'.format(chr(12288),u[0],u[1],u[2],u[3],u[4]))
def main(num):
url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2020.html'
text = getHTMLText(url)
soup = BeautifulSoup(text)
findUnivList(soup)
printAllUniv(num)
main(10)