一、代码详情
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 "error" def fillunivlist(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) alluniv.append(singleuniv) 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 = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html" html=getHTMLText(url) soup=BeautifulSoup(html,"html.parser") fillunivlist(soup) printunivlist(num) main(100)