import requests
import bs4
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 fillunilist(ulist,html):
soup=BeautifulSoup(html,"html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr,bs4.element.Tag):
td=tr('td')
ulist.append([td[0].string,td[1].string,td[2].string])
def show(ulist,num):
tplt="{0:^10}\t{1:^10}\t{2:^10}"
print(tplt.format("排名","学校","总分",chr(12288)))
for i in range(num):
u=ulist[i]
print(tplt.format(u[0],u[1],u[2],chr(12288)))
def main():
uinfo=[]
url="http://www.zuihaodaxue.com/Greater_China_Ranking2019_0.html"
html=gethtmltext(url)
fillunilist(uinfo,html)
show(uinfo,20)
main()
转载:https://blog.csdn.net/qq_37443534/article/details/105232486
查看评论