如何用python代码提取https://www.shanghairanking.cn/rankings/arwu/2020网页里的表格数据
时间: 2023-08-23 22:06:35 浏览: 63
你可以使用Python中的BeautifulSoup库和pandas库来提取该网页中的表格数据。以下是一个示例代码:
```python
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = 'https://www.shanghairanking.cn/rankings/arwu/2020'
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))[0]
print(df)
```
此代码将获取该网页的HTML代码,然后使用BeautifulSoup库在HTML代码中查找第一个表格,最后使用pandas库将表格转换为数据框并打印出来。你可以根据需要修改代码以适应你的实际情况。
相关问题
python采集https://www.shanghairanking.cn/rankings/bcur/202111 这个网站的数据代码
你可以使用requests和BeautifulSoup库来实现对该网站的数据采集和解析。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.shanghairanking.cn/rankings/bcur/202111'
response = requests.get(url, verify=False) # verify=False是为了忽略SSL证书验证,否则会报错
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', class_='rk-table')
rows = table.find_all('tr')
for row in rows:
cols = row.find_all('td')
if len(cols) == 0:
continue
name = cols[0].text.strip()
rank = cols[1].text.strip()
print(name, rank)
```
该代码会输出该网站上所有大学的名称和排名。注意,本示例中需要忽略SSL证书验证,如果你需要更安全的验证方式,请自行更改代码。
用python爬虫链接https://www.shanghairanking.cn/rankings/bcur/2023 返回前30名大学
可以使用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
```