You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从戴尔社区论坛指定日期提取帖子信息并存入Excel?

戴尔社区论坛指定日期评论提取与Excel保存方案

需求明确

  • 提取目标帖子的标题
  • 筛选出2022年10月25日发布的评论,需包含:用户名、完整发布时间、评论内容
  • 将所有符合要求的数据保存至Excel文件

完整实现代码

import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook

url = "https://www.dell.com/community/Inspiron-Desktops/I-am-getting-time-sync-errror-and-the-last-synced-time-shown-as/m-p/8290678#M36017"

# 发送请求并解析页面
result = requests.get(url)
doc = BeautifulSoup(result.text, "html.parser")

# 提取帖子标题
post_title = doc.find("h1", class_="lia-message-subject").get_text(strip=True)

# 初始化存储列表
filtered_comments = []

# 遍历所有评论块
for comment in doc.find_all("div", class_="lia-message-row"):
    # 提取用户名
    username = comment.find("span", class_="lia-user-name").get_text(strip=True)
    # 提取完整发布时间(日期+时间)
    local_date = comment.find("span", class_="local-date").get_text(strip=True)
    local_time = comment.find("span", class_="local-time").get_text(strip=True)
    full_time = f"{local_date} {local_time}"
    # 提取评论内容
    content = comment.find("div", class_="lia-message-body-content").get_text(strip=True, separator="\n")
    
    # 筛选2022年10月25日的评论
    if local_date == "10/25/2022":
        filtered_comments.append([username, full_time, content])

# 创建Excel文件并写入数据
wb = Workbook()
ws = wb.active
# 设置表头
ws.append(["帖子标题", post_title])
ws.append([])  # 空行分隔
ws.append(["用户名", "发布时间", "评论内容"])
# 写入筛选后的评论
for row in filtered_comments:
    ws.append(row)

# 保存Excel文件
wb.save("戴尔论坛评论数据.xlsx")
print("数据已成功保存至Excel文件")

代码说明

  • 页面解析:通过requests获取页面内容,使用BeautifulSoup解析HTML结构
  • 标题提取:定位页面中lia-message-subject类的h1元素,获取帖子标题
  • 评论提取:逐个遍历评论块,从每个块中分别提取用户名、日期、时间及评论内容,合并日期时间为完整格式
  • 日期筛选:判断评论的发布日期是否为目标日期10/25/2022,仅保留符合条件的评论
  • Excel操作:使用openpyxl创建工作簿,写入标题及评论数据,最终保存为Excel文件

内容的提问来源于stack exchange,提问作者NApStor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 06:55:25