使用python爬去中国最好大学排名2016年
吐槽这个排名太扯淡
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except ConnectionError:
return "网络连接出错"
except:
return "未知错误"
def fillUnivList(ulist, html):
soup = BeautifulSoup(html, 'lxml')
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
ulist.append([tds[0].string, tds[1].string, tds[3].string])
def printUnivList(ulist, num):
print("{0:^10} {1:{3}^10} {2:^10}".format("排名", "学校名称", "总分", chr(12288)))
for i in range(num):
u = ulist[i]
print("{0:^10} {1:{3}^10} {2:^10}".format(u[0], u[1], u[2], chr(12288)))
def main():
uinfo = []
url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
html = getHTMLText(url)
fillUnivList(uinfo, html)
printUnivList(uinfo, 20)
if __name__ == "__main__":
main()