Skip to main content

微博社交机器人检测工具包 - Weibo Social Bot Detection Toolkit

Project description

pybotfinder Logo

pybotfinder

微博社交机器人检测工具包 - Weibo Social Bot Detection Toolkit

基于随机森林的微博社交机器人检测系统,提供开箱即用的预测功能。

安装

pip install pybotfinder

快速开始 - 预测

Python API

from pybotfinder import BotPredictor

# 初始化预测器(使用包内默认模型)
predictor = BotPredictor(cookie="YOUR_WEIBO_COOKIE")

# 预测单个用户
result = predictor.predict_from_user_id("1042567781")
print(f"预测结果: {result['prediction']['label_name']}")
print(f"机器人得分: {result['prediction']['bot_score']:.4f}")

命令行

pybotfinder-predict --user-id 1042567781 --cookie "YOUR_WEIBO_COOKIE"

预测结果格式

{
    'user_id': '1042567781',
    'prediction': {
        'label': 1,  # 0=人类, 1=机器人
        'label_name': '机器人',  # '人类' 或 '机器人'
        'bot_score': 0.95  # 机器人得分(范围0-1,值越大表示越可能是机器人)
    }
}

返回值说明:

  • label: 预测标签,0表示人类,1表示机器人
  • label_name: 标签名称,'人类' 或 '机器人'
  • bot_score: 机器人得分,是模型预测该账号为机器人的概率值(范围0-1)
    • bot_score 接近1表示模型认为该账号很可能是机器人
    • bot_score 接近0表示模型认为该账号很可能是人类
    • 注意bot_score 是模型输出的概率值,不是统计意义上的置信区间,应结合具体应用场景理解

训练数据来源

模型基于以下数据训练:

  • 机器人样本 (4480个账号ID): 来自 bot.txt,主要为自动化营销账号和少部分LLM驱动的机器人账号
  • 人类样本 (4578个账号ID): 来自以下文件:
    • human.txt (2269个): 普通用户账号,通过实时推文中以常用虚词为搜索词采集
    • government.txt (597个): 政府机构账号,通过微博政务榜单采集
    • influencer.txt (931个): 影响者账号,通过微博V影响力榜单采集
    • media.txt (781个): 媒体账号,以"新闻"为关键词搜索相关的认证账号采集

标签分布说明:

  • 由于部分账号在数据采集时可能已被封禁、删除或设置为私密,实际成功提取特征并用于训练的样本数可能少于账号ID总数
  • 实际成功提取特征的样本总数:约9060个
  • 数据集划分:训练集约7248个(80%),测试集1812个(20%)
  • 模型训练时会自动过滤无法采集到profile数据的账号

训练特征

模型使用33个特征进行训练,包括:

Profile-level特征 (10个)

  1. 昵称特征 (2个):

    • screen_name_length: 昵称长度
    • screen_name_digit_count: 昵称中数字数量
  2. 描述特征 (2个):

    • description_length: 描述长度
    • description_has_sensitive_word: 描述中是否包含敏感词(1=是,0=否)
  3. 基本统计 (5个):

    • gender_n: 性别(未知=1,其他=0)
    • followers_count: 粉丝数
    • friends_count: 关注数
    • followers_friends_ratio: 粉丝/关注比例
    • statuses_count: 微博总数
  4. 视觉特征 (1个):

    • is_default_avatar: 是否使用默认头像(1=是,0=否)

Posts-level特征 (23个)

  1. 基本统计 (2个):

    • posts_count: 收集到的帖子数量
    • original_ratio: 原创微博比例
  2. 原创内容特征 (6个,保留重要的均值和标准差):

    • avg_text_length_original: 平均文本长度
    • avg_punctuation_count_original: 平均标点符号数
    • std_punctuation_count_original: 标点符号数标准差
    • avg_pic_count_original: 平均图片数
    • avg_video_count_original: 平均视频数
    • std_video_count_original: 视频数标准差
  3. 原创互动特征 (6个,均值和标准差):

    • avg_likes_original: 原创微博平均点赞数
    • std_likes_original: 原创微博点赞数标准差
    • avg_reposts_original: 原创微博平均转发数
    • std_reposts_original: 原创微博转发数标准差
    • avg_comments_original: 原创微博平均评论数
    • std_comments_original: 原创微博评论数标准差
  4. 时间特征 (5个):

    • avg_post_interval: 平均发帖间隔(秒)
    • std_post_interval: 发帖间隔标准差(秒)
    • peak_hourly_posts: 峰值小时发帖数
    • peak_daily_posts: 峰值日发帖数
    • avg_daily_posts: 平均每天发帖数量
  5. 情感特征 (2个,基于SnowNLP,只保留平均值):

    • avg_sentiment_positive: 积极情感平均值
    • avg_sentiment_negative: 消极情感平均值
  6. 其他特征 (2个):

    • location_ratio: 包含地理位置的微博比例
    • repost_user_entropy: 转发用户信息熵

模型评估

整体性能

  • 准确率 (Accuracy): 98.29%
  • 测试集F1分数 (宏平均): 0.9829
  • 测试集F1分数 (加权平均): 0.9829

各类别性能

人类 (类别0):

  • 精确率 (Precision): 0.9702
  • 召回率 (Recall): 0.9967
  • F1-score: 0.9833

机器人 (类别1):

  • 精确率 (Precision): 0.9966
  • 召回率 (Recall): 0.9688
  • F1-score: 0.9825

注意事项

  1. Cookie获取:

    • Cookie需要从微博网页版获取
    • 访问 https://weibo.com 并登录
    • 打开浏览器开发者工具(F12)
    • 在Network标签中找到任意请求,复制请求头中的Cookie值
    • Cookie格式示例: SUB=xxx; SUBP=xxx; ...
    • Cookie用于访问微博API采集用户数据,请妥善保管
  2. 模型文件:

    • 包内已包含训练好的模型文件 (bot_detection_model.pkl)
    • 无需额外下载或训练,安装后即可使用
  3. 数据采集:

    • 预测时需要采集用户数据,请确保网络连接正常
    • 采集过程可能需要几秒钟时间
    • 重要:如果用户的profile数据采集失败(账号不存在、被封禁或Cookie无效),预测将返回错误信息,不会进行预测
  4. 返回值说明:

    • label: 预测标签,0表示人类,1表示机器人
    • label_name: 标签名称,'人类' 或 '机器人'
    • bot_score: 机器人得分,是模型预测该账号为机器人的概率值(范围0-1)
      • bot_score 接近1表示模型认为该账号很可能是机器人
      • bot_score 接近0表示模型认为该账号很可能是人类
      • 重要提示bot_score 是模型输出的概率值,不是统计意义上的置信区间或假设检验的p值,应结合具体应用场景理解和使用,避免过度解读
  5. 模型评估说明:

    • 模型评估结果只代表在测试集上的表现,不代表真实情境表现

许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybotfinder-0.2.5.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybotfinder-0.2.5-py3-none-any.whl (1.7 MB view details)

Uploaded Python 3

File details

Details for the file pybotfinder-0.2.5.tar.gz.

File metadata

  • Download URL: pybotfinder-0.2.5.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for pybotfinder-0.2.5.tar.gz
Algorithm Hash digest
SHA256 f754360abddebca058132e7e4ab034cdb2ca610bb3e553786564e44b21111a1d
MD5 71a044603722577198bed920d8a20d52
BLAKE2b-256 9261e5516e11212c4941b37cb89cf6f3e71377cec065def21bc3d1f8dd6c07f4

See more details on using hashes here.

File details

Details for the file pybotfinder-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: pybotfinder-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for pybotfinder-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d70617e2d4db444e0547851dd9a2dbe41f683c9a2dd59673482f5fc2d8e6a27f
MD5 124ea95475a6d6404061c37a36641e31
BLAKE2b-256 1c46f6092bba7bf709cd5a325e62820718bd71b305893529b4d08d267f75dffc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page