北理工Python语言程序设计基础(第2版) 教材采用的学校比较多,第十章实例20,中国大学排名爬虫程序,因为网站改版,书上代码无法运行,过年没事根据新网站修改了一下程序,给需要的学生们参考。
import requests
from bs4 import BeautifulSoup
import re
allUniv = []
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = 'utf-8'
return r.text
except:
return ""
def fillUnivList(soup):
data = soup.find_all('tr')
for tr in range(1,len(data)):
#ltd = tr.find_all('td')
singleUniv = []
#for td in ltd:
for td in range(6):
temp="".join(re.compile('[\d+(\.\d+)?(\u4e00-\u9fa5)]').findall(data[tr].contents[td].text))
temp=re.sub("[()]","",temp)
#singleUniv.append(td.string)
singleUniv.append(temp)
allUniv.append(singleUniv)
def printUnivList(num):
#print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))
print("{1:{0}>3}{2:{0}^9}{3:{0}^5}{4:{0}^5}{5:{0}^5}{6:{0}^4}".format(chr(12288),"排名","学校名称","省市","类型","总分","生源质量"))
for i in range(num):
u=allUniv[i]
print("{1:>4}{2:{0}^10}{3:{0}^5}{4:{0}^5}{5:{0}^5}{6:{0}^8}".format(chr(12288),u[0],u[1],u[2],u[3],u[4],u[5]))
def main():
url = 'https://www.shanghairanking.cn/rankings/bcur/201611'
html = getHTMLText(url)
soup = BeautifulSoup(html, "html.parser")
fillUnivList(soup)
printUnivList(30)
main()
2021-02-16
最新推荐文章于 2022-10-12 23:19:42 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)