Skip to content

Python + weibo-scraper #32

Description

@xinanchen83-ui

===============================================

微博数据爬虫模板(Python + weibo-scraper)

主题:人工智能教育公众话语

输出:Excel 文件,包含分析所需字段

===============================================

from weibo_scraper import get_formatted_weibo_tweets_by_name
import pandas as pd
from datetime import datetime

1️⃣ 设置关键词(可以扩展)

keywords = ['人工智能教育', 'AI教师', 'AI学习', '智能课堂', '教育数字化']

2️⃣ 设置采集参数

pages_per_keyword = 5 # 每个关键词抓取页数,可根据需要调整
start_date = '2022-01-01'
end_date = '2025-10-01'

3️⃣ 定义列表保存数据

data_list = []

4️⃣ 循环关键词采集

for kw in keywords:
print(f"正在采集关键词:{kw}")
weibo_data = get_formatted_weibo_tweets_by_name(name=kw, pages=pages_per_keyword)

for user_meta in weibo_data:
    for card in user_meta.cards_node:
        tweet = card.mblog

        # 过滤时间(确保在指定时间范围内)
        try:
            pub_date = datetime.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0800 %Y')
        except:
            # 若格式异常,可跳过
            continue
        if pub_date < datetime.strptime(start_date, '%Y-%m-%d') or pub_date > datetime.strptime(end_date, '%Y-%m-%d'):
            continue

        # 5️⃣ 提取字段
        data_dict = {
            '微博文本': tweet.text,                 # 核心分析文本
            '发布时间': pub_date.strftime('%Y-%m-%d'),  # 时间趋势分析
            '点赞数': tweet.attitudes_count,         # 互动量,可作为热门话题参考
            '评论数': tweet.comments_count,          # 互动量
            '转发数': tweet.reposts_count,           # 互动量
            '关键词': kw,                            # 哪个关键词触发
            '初步分类': '',                          # 手动或后续分析添加:期待/焦虑/中性
        }
        data_list.append(data_dict)

6️⃣ 保存到 Excel

df = pd.DataFrame(data_list)

输出文件名

output_file = '微博_AI教育语料.xlsx'
df.to_excel(output_file, index=False)

print(f"数据采集完成,共采集 {len(df)} 条微博,保存至 {output_file}")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions