pycharm简单爬取https://www.shanghairanking.com/rankings/bcur/2023学校排名
时间: 2023-10-05 13:06:12 浏览: 177
你可以使用Python的requests和BeautifulSoup库来实现简单的爬取。以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.shanghairanking.com/rankings/bcur/2023"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
table = soup.find("table", {"id": "UniversityRanking"})
rows = table.find_all("tr")
for row in rows:
cols = row.find_all("td")
if len(cols) > 0:
rank = cols[0].text.strip()
name = cols[1].text.strip()
print(f"{rank}: {name}")
```
该代码首先使用requests库获取网页的HTML内容,然后使用BeautifulSoup库解析HTML。接着,我们找到排名表格的HTML元素,并提取表格中的行和列。最后,我们循环遍历每一行,并提取排名和学校名称,将它们打印出来。
需要注意的是,为了避免被网站屏蔽,我们应该加入适当的延迟和请求头部信息。
相关问题
用pycharm scrapy框架爬取https://www.shanghairanking.cn/institution校名、地区、管理部门、类别、双一流的内容并写入csv文件
好的,以下是一个使用Scrapy框架的示例代码,您可以在PyCharm中创建一个新的Scrapy项目并将其替换为spider.py文件:
```python
import scrapy
import csv
class UniversitySpider(scrapy.Spider):
name = "university"
start_urls = ["https://www.shanghairanking.cn/institution"]
def parse(self, response):
for university in response.css("table#UniversityRanking tbody tr"):
name = university.css("td[class*=left] a::text").get().strip()
region = university.css("td")[1].css("a::text").get().strip()
department = university.css("td")[2].css("a::text").get().strip()
category = university.css("td")[3].css("a::text").get().strip()
double_first_class = university.css("td")[4].css("a::text").get().strip()
# 将数据写入CSV文件
with open("universities.csv", "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([name, region, department, category, double_first_class])
# 获取下一页链接并继续爬取
next_page = response.css("div.pagination a.next::attr(href)").get()
if next_page is not None:
yield response.follow(next_page, self.parse)
```
这段代码将使用Scrapy框架爬取上海交通大学排名网站的内容,并将校名、地区、管理部门、类别、双一流写入CSV文件中。请确保您已经安装了Scrapy和csv模块,并将项目文件夹中的settings.py中的ROBOTSTXT_OBEY设置为False,以允许爬取。
用pycharm爬取https://www.shanghairanking.cn/institution校名、地区、管理部门、类别、双一流的内容并写入csv文件的代码
以下是使用Python 3和BeautifulSoup库爬取https://www.shanghairanking.cn/institution网站的校名、地区、管理部门、类别、双一流的信息并将其写入CSV文件的代码。
```
import requests
from bs4 import BeautifulSoup
import csv
# 网站的URL
url = "https://www.shanghairanking.cn/institution"
# 发送GET请求
response = requests.get(url)
# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(response.content, 'html.parser')
# 获取所有学校的信息
schools = soup.find_all('tr', class_='bgfd')
# 创建CSV文件并写入表头
with open('shanghai_rankings.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['校名', '地区', '管理部门', '类别', '双一流'])
# 遍历每个学校并写入CSV文件
for school in schools:
name = school.find('td', class_='left').text.strip()
region = school.find('td', class_='region').text.strip()
department = school.find('td', class_='department').text.strip()
category = school.find('td', class_='category').text.strip()
double_first_class = school.find('td', class_='dual-class').text.strip()
writer.writerow([name, region, department, category, double_first_class])
print('数据已成功写入CSV文件!')
```
这段代码将会爬取学校名单并将其写入名为`shanghai_rankings.csv`的CSV文件。请注意,这个代码只是一个简单的示例,你可能需要做一些修改以适应你的具体情况。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](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)