news_list = soup.find_all("a", class_="news_list_title") for news in news_list: title = news.get_text() link = news.get("href") news_response = requests.get(link) news_soup = BeautifulSoup(news_response.content, "html.parser") content = news_soup.find("div", class_="news_content").get_text() if "公示" in title: ws.append([title, link, content])
时间: 2024-03-18 15:43:43 浏览: 16
这段代码的作用是爬取一个网页中的新闻列表,然后获取每个新闻的标题、链接和内容,并将包含关键词“公示”的新闻信息写入一个 Excel 表格中。具体来说,代码通过 BeautifulSoup 库解析 HTML 页面,获取 class 属性为"news_list_title"的所有a标签,然后遍历每个a标签,获取其标题和链接,随后请求链接,解析新闻详细页面,获取新闻内容,最后判断标题中是否包含关键词“公示”,如果包含,则将新闻的标题、链接和内容写入一个 Excel 表格中。
相关问题
修改这段代码并写出来import requests from bs4 import BeautifulSoup # 爬取的页面数量 num_pages = 5 # 打开每个页面并获取 HTML 内容 for page_num in range(1, num_pages + 1): url = f"https://www.bernama.com/bm/index.php/archive.php?page={page_num}" response = requests.get(url) html_content = response.text # 解析 HTML 内容,获取新闻标题和内容 soup = BeautifulSoup(html_content, "html.parser") news_list = soup.find_all("div", class_="row news-row") for news in news_list: title = news.find("a").text.strip() link = news.find("a")["href"] news_response = requests.get(link) news_html = news_response.text news_soup = BeautifulSoup(news_html, "html.parser") content_list = news_soup.find_all("div", class_="col-md-12 news-body") content = "\n".join([p.text for p in content_list[0].find_all("p")]) print(content) # 将新闻标题和内容保存到 txt 文件 with open(f"page{page_num}_{title}.txt", "w") as f: f.write(f"Title: {title}\n") f.write(f"Link: {link}\n") f.write(f"Content:\n{content}\n")
import requests
from bs4 import BeautifulSoup
# 爬取的页面数量
num_pages = 5
# 打开每个页面并获取 HTML 内容
for page_num in range(1, num_pages + 1):
url = f"https://www.bernama.com/bm/index.php/archive.php?page={page_num}"
response = requests.get(url)
html_content = response.text
# 解析 HTML 内容,获取新闻标题和内容
soup = BeautifulSoup(html_content, "html.parser")
news_list = soup.find_all("div", class_="row news-row")
for news in news_list:
title = news.find("a").text.strip()
link = news.find("a")["href"]
# 打开每个新闻链接并获取 HTML 内容
news_response = requests.get(link)
news_html = news_response.text
# 解析新闻 HTML 内容,获取新闻内容
news_soup = BeautifulSoup(news_html, "html.parser")
content_list = news_soup.find_all("div", class_="col-md-12 news-body")
content = "\n".join([p.text for p in content_list[0].find_all("p")])
# 将新闻标题和内容保存到 txt 文件
with open(f"page{page_num}_{title}.txt", "w") as f:
f.write(f"Title: {title}\n")
f.write(f"Link: {link}\n")
f.write(f"Content:\n{content}\n")
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)
这段代码用于爬取中国绿色金融网站上的政策信息,并输出政策标题和发布时间。
如果您在 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 文件中。请注意,您需要在代码中指定正确的文件路径,以便能够将数据写入到正确的文件中。