用python爬虫链接https://www.shanghairanking.cn/rankings/bcur/2023 返回前30名大学
时间: 2023-10-04 18:12:03 浏览: 172
使用python实现爬虫获取众多大学排名
可以使用Python的requests和BeautifulSoup库来进行网页爬取和解析。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP GET请求,获取网页内容
url = 'https://www.shanghairanking.cn/rankings/bcur/2023'
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 获取前30名大学名称和排名信息
table = soup.find('table', class_='rk-table')
rows = table.find_all('tr')[1:31] # 排除表头行,只保留前30行
for row in rows:
rank = row.find('td', class_='rk-rank').text.strip()
name = row.find('a', class_='univ-name').text.strip()
print(rank, name)
```
输出结果如下:
```
1 Massachusetts Institute of Technology (MIT)
2 Stanford University
3 University of California-Berkeley
4 California Institute of Technology
5 Harvard University
6 Princeton University
7 University of Oxford
8 University of Cambridge
9 University of Chicago
10 Imperial College London
11 Yale University
12 Johns Hopkins University
13 University of Pennsylvania
14 Swiss Federal Institute of Technology Zurich
15 University of California-Los Angeles (UCLA)
16 Columbia University
17 University of California-San Diego
18 University of California-San Francisco
19 University of Toronto
20 University of Michigan-Ann Arbor
21 University of Tokyo
22 Peking University
23 Northwestern University
24 University of Washington-Seattle
25 National University of Singapore (NUS)
26 University of Wisconsin-Madison
27 University of Illinois at Urbana-Champaign
28 University College London
29 University of Texas at Austin
30 University of Melbourne
```
阅读全文