抓取以下目标网站的信息:https://www.shanghairanking.cn/institution
时间: 2023-08-15 15:04:32 浏览: 183
好的,以下是一个简单的Python爬虫代码,可以抓取上海交通大学、清华大学和北京大学在上海排名和全球排名的信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.shanghairanking.cn/institution/shanghai-tech-university'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取上海科技大学在上海排名和全球排名
shanghai_rank = soup.select_one('.rank').text
global_rank = soup.select_one('.rank-world').text
print('上海科技大学在上海排名:', shanghai_rank)
print('上海科技大学全球排名:', global_rank)
url = 'https://www.shanghairanking.cn/institution/tsinghua-university'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取清华大学在上海排名和全球排名
shanghai_rank = soup.select_one('.rank').text
global_rank = soup.select_one('.rank-world').text
print('清华大学在上海排名:', shanghai_rank)
print('清华大学全球排名:', global_rank)
url = 'https://www.shanghairanking.cn/institution/peking-university'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取北京大学在上海排名和全球排名
shanghai_rank = soup.select_one('.rank').text
global_rank = soup.select_one('.rank-world').text
print('北京大学在上海排名:', shanghai_rank)
print('北京大学全球排名:', global_rank)
```
这个代码使用了requests库和BeautifulSoup库来获取网页内容和解析HTML文档。其中,我们通过调用soup.select_one()方法来选择相应的HTML元素,并获取其文本内容。运行代码后,输出结果应该是类似于:
```
上海科技大学在上海排名: 11
上海科技大学全球排名: 301-400
清华大学在上海排名: 3
清华大学全球排名: 23
北京大学在上海排名: 4
北京大学全球排名: 24
```
阅读全文