python爬虫使用beautifulsoup库爬取www.shanghairanking.cn网站中 大学名字 分数排名
时间: 2023-12-25 12:06:53 浏览: 41
好的,以下是使用BeautifulSoup库爬取www.shanghairanking.cn网站中大学名字分数排名的Python代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.shanghairanking.cn/rankings/bcur/2021'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find_all('table', class_='rk-table')[0]
tbody = table.find_all('tbody')[0]
rows = tbody.find_all('tr')
for row in rows:
cols = row.find_all('td')
name = cols[1].text.strip()
score = cols[2].text.strip()
print(name, score)
```
这段代码首先使用requests库发送HTTP GET请求获取网页内容,然后使用BeautifulSoup库解析网页内容。接着,我们找到网页中的表格元素,并遍历表格中的每一行,提取每行中的大学名字和分数排名,并打印输出。注意,这里使用的是2021年的排名数据,如果需要获取其他年份的排名数据,只需要修改url即可。
相关问题
python爬虫使用beautifulsoup库爬取www.shanghairanking.cn网站中 大学排名
好的,我会尝试为您解答关于使用beautifulsoup库爬取www.shanghairanking.cn网站中大学排名的问题。首先,您需要安装beautifulsoup库并导入需要的模块,例如以下代码:
```
from urllib.request import urlopen
from bs4 import BeautifulSoup
```
接着,您可以使用urlopen()函数打开目标网址,然后使用BeautifulSoup模块解析HTML代码。以下是一个简单的示例:
```
html = urlopen("http://www.shanghairanking.cn/rankings/bcur/2021")
bsobj = BeautifulSoup(html, "html.parser")
```
在上面的代码中,我们使用了urlopen()函数打开了目标网址,然后使用BeautifulSoup模块将HTML代码解析成了一个BeautifulSoup对象。接下来,您可以使用find()或find_all()方法查找需要的标签,例如以下代码可以提取出目标网页中所有的大学排名信息:
```
table = bsobj.find("table", {"class":"tbls"})
rows = table.find_all("tr")
for row in rows:
cols = row.find_all("td")
for col in cols:
print(col.get_text(), end="\t")
print("\n")
```
在上面的代码中,我们先使用find()方法查找了class为“tbls”的table标签,然后使用find_all()方法查找了所有的tr标签,接着使用一个循环依次提取出每一个td标签中的文本信息并进行输出。当然,您还可以根据自己的需求对提取出的信息进行进一步的处理。
用python爬虫爬取https://www.shanghairanking.cn/institution网站的内容并写入csv文件的代码
好的,以下是代码实现:
```python
import requests
from bs4 import BeautifulSoup
import csv
# 获取网页内容
url = "https://www.shanghairanking.cn/institution"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 获取表格头
table_head = soup.find("thead").find_all("th")
headers = [th.text.strip() for th in table_head]
# 获取表格数据
table_body = soup.find("tbody").find_all("tr")
data = []
for tr in table_body:
row = []
tds = tr.find_all("td")
for td in tds:
row.append(td.text.strip())
data.append(row)
# 写入csv文件
with open("shanghai_ranking.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(data)
```
这段代码使用requests和BeautifulSoup库获取了目标网站的内容,然后从中提取出表格头和表格数据,最后将它们写入到CSV文件中。注意要在代码中指定文件名和编码格式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)