#爬取2022中国大学排名信息
import bs4
import requests
import re
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 ""
return ""
def fillUnivList(ulist, html):
# 提取网页内容中信息到合适的数据结构
soup = BeautifulSoup(html, 'html.parser')
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
school_id = tds[0]('div')[0].string.strip() #排名
school_name = tds[1]('a')[0].string.strip()
school_score = tds[4].string.strip()
school_info = [school_id, school_name, school_score]
ulist.append(school_info)
def printUnivList(ulist, num):
#利用数据结构展示并输出结果
tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}
Python爬取大学排名
于 2022-05-17 11:04:54 首次发布