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 是模型输出的概率值,不是统计意义上的置信区间,应结合具体应用场景理解

训练数据来源

模型基于以下数据训练:

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

总样本数: 8424个用户

训练特征

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

Profile-level特征 (21个)

  1. 昵称特征 (4个):

    • screen_name_length: 昵称长度
    • screen_name_digit_count: 昵称中数字数量
    • screen_name_letter_count: 昵称中字母数量
    • screen_name_has_special_char: 昵称中是否包含特殊字符(1=是,0=否)
  2. 描述特征 (9个):

    • description_length: 描述长度
    • description_has_sensitive_word: 描述中是否包含敏感词(1=是,0=否)
    • description_has_url: 描述中是否包含URL(1=是,0=否)
    • description_has_at: 描述中是否包含@(1=是,0=否)
    • description_has_hash: 描述中是否包含#(1=是,0=否)
    • description_has_digit: 描述中是否包含数字(1=是,0=否)
    • description_has_letter: 描述中是否包含字母(1=是,0=否)
    • description_has_special_char: 描述中是否包含特殊字符(1=是,0=否)
  3. 基本统计 (7个):

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

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

Posts-level特征 (26个)

  1. 基本统计 (2个):

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

    • avg_text_length_original: 平均文本长度
    • std_text_length_original: 文本长度标准差
    • avg_punctuation_count_original: 平均标点符号数
    • std_punctuation_count_original: 标点符号数标准差
    • avg_pic_count_original: 平均图片数
    • std_pic_count_original: 图片数标准差
    • avg_video_count_original: 平均视频数
    • std_video_count_original: 视频数标准差
    • avg_link_count_original: 平均链接数
    • std_link_count_original: 链接数标准差
    • avg_at_count_original: 平均@数量
    • std_at_count_original: @数量标准差
    • avg_hash_count_original: 平均#数量
    • std_hash_count_original: #数量标准差
  3. 时间特征 (4个):

    • avg_post_interval: 平均发帖间隔(秒)
    • std_post_interval: 发帖间隔标准差(秒)
    • peak_hourly_posts: 峰值小时发帖数
    • peak_daily_posts: 峰值日发帖数
  4. 情感特征 (4个,基于SnowNLP):

    • avg_sentiment_positive: 积极情感平均值
    • std_sentiment_positive: 积极情感标准差
    • avg_sentiment_negative: 消极情感平均值
    • std_sentiment_negative: 消极情感标准差
  5. 其他特征 (2个):

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

模型评估

整体性能

  • 准确率 (Accuracy): 98.93%
  • 交叉验证F1分数: 0.9851 (±0.0028)
  • 测试集F1分数 (宏平均): 0.9892
  • 测试集F1分数 (加权平均): 0.9893

各类别性能

人类 (类别0):

  • 精确率 (Precision): 0.9828
  • 召回率 (Recall): 0.9978
  • F1-score: 0.9902

机器人 (类别1):

  • 精确率 (Precision): 0.9974
  • 召回率 (Recall): 0.9785
  • F1-score: 0.9879

注意事项

  1. Cookie获取:

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

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

    • 预测时需要采集用户数据,请确保网络连接正常
    • 采集过程可能需要几秒钟时间
  4. 返回值说明:

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

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

许可证

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

作者

Xiao MENG - xiaomeng7-c@my.cityu.edu.hk

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.0.tar.gz (650.5 kB 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.0-py3-none-any.whl (671.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pybotfinder-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d0572e2029859090875309b39d9d2f3275d3fb243b255049b3efb2a2a1e58ca3
MD5 fc2c5d1f5643e895fe1ef25853cbad3c
BLAKE2b-256 aeadaa98df27f4b64c31223fb1d3b7d477b944bec2587a3dc0256908d9226d37

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybotfinder-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 671.6 kB
  • 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c1e2dac889c3be8ee6ec6a53075ec301b94cb35ff271ee64a2a268755d634d0
MD5 a85995c49df2b38981615fad98bf644a
BLAKE2b-256 75b701bea8415141ff316bc2bf2bd5a0296628ee14736b258d6309f65b4c9728

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