1. 學習視頻
中國大學排名定向爬蟲:
- https://www.bilibili.com/video/BV1pt41137qK?p=33&spm_id_from=pageDriver
2. 程式設計
輸入:https://www.shanghairanking.cn/rankings/bcur/2021
輸出:大學排名資訊的列印(排名、大學名稱、總分)
技術路線:requests-bs4
定向爬蟲:僅對輸入URL進行爬取,不拓展爬取
- 從網路上獲取大學排名網頁內容
- 提取網頁內容中資訊到合適的資料結構
- 利用資料結構展開并輸出結果
3. 需求
獲取該網頁的排行資訊

4. 網頁源代碼查看大學資訊的節點
先找到 tbody 標簽,獲取所有大學
再找到 tr 標簽,獲取具體大學
最后找到 td,獲取大學詳情

先找到 tbody 標簽,獲取所有大學
再找到 tr 標簽,獲取具體大學
最后找到 td,獲取大學詳情
<tbody data-v-6885f26d>
<tr data-v-6885f26d>
<td data-v-6885f26d>
<div class="ranking top1" data-v-6885f26d>1</div>
</td>
<td class="align-left" data-v-6885f26d>
<div class="univname-container" data-v-6885f26d>
<div class="logo" data-v-6885f26d>
<img alt="清華大學" one rror='this.src="/images/blank.svg"' src="https://www.shanghairanking.cn/_uni/logo/27532357.png" class="univ-logo" data-v-6885f26d>
</div>
<div class="univname" data-v-6885f26d>
<div data-v-b80b4d60 data-v-6885f26d>
<div class="tooltip" data-v-b80b4d60>
<div class="link-container" data-v-b80b4d60>
<a
href="/institution/tsinghua-university" class="name-cn" data-v-b80b4d60>清華大學
</a>
<div
class="collection" style="display:none" data-v-b80b4d60><img src="/_nuxt/img/uncollection.5e124aa.svg" alt data-v-b80b4d60>
</div>
</div>
</div>
</div>
<div data-v-f9104fdc data-v-6885f26d>
<div class="tooltip" data-v-f9104fdc>
<div class="link-container" data-v-f9104fdc>
<a
href="/institution/tsinghua-university" class="name-en" data-v-f9104fdc>Tsinghua University
</a>
</div>
</div>
</div>
</td>
<td data-v-6885f26d>北京</td>
<td data-v-6885f26d>綜合</td>
<td data-v-6885f26d>969.2</td>
<td data-v-6885f26d>37.9</td>
</tr>
5. 代碼示例
import requests
from bs4 import BeautifulSoup
import bs4
"""
中國大學排名定向爬蟲:https://www.bilibili.com/video/BV1pt41137qK?p=33&spm_id_from=pageDriver
輸入:https://www.shanghairanking.cn/rankings/bcur/2021
輸出:大學排名資訊的列印(排名、大學名稱、總分)
技術路線:requests-bs4
定向爬蟲:僅對輸入URL進行爬取,不拓展爬取
程式設計
1. 從網路上獲取大學排名網頁內容
2. 提取網頁內容中資訊到合適的資料結構
3. 利用資料結構展開并輸出結果
"""
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
return ""
# 提取HTML關鍵資訊,決議出大學名稱,排名,分數
def fillUnivList(list, html):
soup = BeautifulSoup(html, "html.parser")
# 1. 先找到 tbody 標簽,獲取所有大學
for tr in soup.find('tbody').children:
# 2. 再找到 tr 標簽,獲取具體大學
if isinstance(tr, bs4.element.Tag):
# 3. 最后找到 td,獲取大學詳情
tds = tr('td')
uRank = tds[0].string.replace('\n', "").strip() # 去除換行符和空格
uName = tds[1].div.a.string.replace('\n', "").strip()
uScore = tds[4].string.replace('\n', "").strip()
list.append([uRank, uName, uScore])
def printList(list, num):
mFormat = "{0:^10}\t{1:^10}\t{2:^15}"
print(mFormat.format("排名", "學校名稱", "總分"), chr(12288))
for i in range(num):
info = list[i]
print(mFormat.format(info[0], info[1], info[2]), chr(12288))
if __name__ == '__main__':
list = []
url = "https://www.shanghairanking.cn/rankings/bcur/2021"
html = getHTMLText(url)
fillUnivList(list, html)
# 列出20所大學的排名資訊
printList(list, 20)
6. 運行結果
對照結果和網頁的實際顯示,查看結果為一致
C:\Users\珞落\AppData\Local\Programs\Python\Python39\python.exe D:/PythonProject/main.py
排名 學校名稱 總分
1 清華大學 969.2
2 北京大學 855.3
3 浙江大學 768.7
4 上海交通大學 723.4
5 南京大學 654.8
6 復旦大學 649.7
7 中國科學技術大學 577
8 華中科技大學 574.3
9 武漢大學 567.9
10 西安交通大學 537.9
11 哈爾濱工業大學 522.6
12 中山大學 519.3
13 北京師范大學 518.3
14 四川大學 516.6
15 北京航空航天大學 513.8
16 同濟大學 508.3
17 東南大學 488.1
18 中國人民大學 487.8
19 北京理工大學 474
20 南開大學 465.3
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/392128.html
標籤:python
