import requests from bs4 import BeautifulSoup url = 'http://www.greenfinancechina.com/zhengcefagui/list-1.html' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 获取政策标题和发布时间 policy_list = soup.find_all('div', class_='news-item') for policy in policy_list: title = policy.find('a').text time = policy.find('span', class_='time').text print(title, time)
时间: 2023-12-31 22:06:41 浏览: 86
这段代码用于爬取中国绿色金融网站上的政策信息,并输出政策标题和发布时间。
如果您在 Spyder 中执行此代码,应该能够看到类似以下内容的输出:
```python
关于全面启动绿色信贷政策试点工作的通知 2021-11-02
关于发布《绿色债务融资外部评估管理办法》的公告 2021-11-02
...
```
这些输出显示了政策标题和发布时间。如果您想要保存这些政策信息,可以将输出写入文件中。例如,您可以将以下代码添加到循环中,将政策信息写入 CSV 文件中:
```python
import csv
# 打开 CSV 文件
with open('policies.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['title', 'time']) # 写入表头
# 写入政策信息
for policy in policy_list:
title = policy.find('a').text
time = policy.find('span', class_='time').text
writer.writerow([title, time])
```
这样,政策信息就会被写入 CSV 文件中。请注意,您需要在代码中指定正确的文件路径,以便能够将数据写入到正确的文件中。
相关问题
import requests from bs4 import BeautifulSoup url = 'http://www.greenfinancechina.com/zhengcefagui/list-1.html' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 获取政策标题和发布时间 policy_list = soup.find_all('div', class_='news-item') for policy in policy_list: title = policy.find('a').text time = policy.find('span', class_='time').text print(title, time)
这段代码可以用来爬取中国绿色金融网站上的政策信息,并将政策标题和发布时间输出到控制台上。
您可以将这段代码保存为一个 Python 文件,然后在终端或者命令行中执行该文件。请注意,为了能够正常运行该代码,您需要安装 requests 和 BeautifulSoup 这两个库。您可以使用以下命令来安装这两个库:
```bash
pip install requests
pip install beautifulsoup4
```
如果您已经安装了这两个库,就可以运行该代码了。运行代码时,可能会遇到一些错误,例如网络连接错误或者解析错误。您可以根据错误提示修改代码或者调整网络设置,以便能够成功爬取政策信息。
修改代码,使得li_list的编码格式是utf-8import requests from bs4 import BeautifulSoup url = 'https://www.icbc.com.cn/page/827855918799994880.html' response = requests.get(url=url) page_response = response.text soup = BeautifulSoup(page_response, 'html.parser',from_encoding='utf-8') li_list = soup.select('#mypagehtmlcontent p')
import requests
from bs4 import BeautifulSoup
url = 'https://www.icbc.com.cn/page/827855918799994880.html'
response = requests.get(url=url)
page_response = response.content.decode('utf-8')
soup = BeautifulSoup(page_response, 'html.parser')
li_list = soup.select('#mypagehtmlcontent p')
# 以下为测试代码,可以不用理会
for li in li_list:
print(li.text)
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)