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 09:43:43 浏览: 148
这段代码的作用是爬取一个网页中的新闻列表,然后获取每个新闻的标题、链接和内容,并将包含关键词“公示”的新闻信息写入一个 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/en/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")]) # 将新闻标题和内容保存到 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")修改这段代码
这段代码是一个Python爬虫,用于从https://www.bernama.com/en/archive.php网站上爬取新闻标题和内容,并将其保存到txt文件中。在修改这段代码之前,需要先确定你想要做什么,比如想要添加或修改哪些功能。以下是一些可能的修改方案:
1. 添加异常处理机制,以防止在爬取页面或解析HTML时出现错误,例如添加try-except块。
2. 添加用户代理,以避免被该网站封锁IP地址。
3. 将txt文件保存到其他文件格式中,例如CSV或JSON。
4. 添加多线程或异步处理,以提高爬取效率。
5. 修改爬取页面的数量或URL,以获取不同的新闻数据。
6. 修改保存的文件名格式或路径,以更好地组织保存的数据。
7. 添加其他功能,例如将新闻数据存储到数据库中,或根据新闻内容进行文本分析等。
修改这段代码并写出来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")
阅读全文
相关推荐
















