微博社交机器人检测工具包 - Weibo Social Bot Detection Toolkit
Project description
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个用户
训练特征
模型使用40个特征进行训练,包括:
Profile-level特征 (10个)
-
昵称特征 (2个):
screen_name_length: 昵称长度screen_name_digit_count: 昵称中数字数量
-
描述特征 (2个):
description_length: 描述长度description_has_sensitive_word: 描述中是否包含敏感词(1=是,0=否)
-
基本统计 (5个):
gender_n: 性别(未知=1,其他=0)followers_count: 粉丝数friends_count: 关注数followers_friends_ratio: 粉丝/关注比例statuses_count: 微博总数
-
视觉特征 (1个):
is_default_avatar: 是否使用默认头像(1=是,0=否)
Posts-level特征 (30个)
-
基本统计 (2个):
posts_count: 收集到的帖子数量original_ratio: 原创微博比例
-
原创内容特征 (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: 视频数标准差
-
时间特征 (4个):
avg_post_interval: 平均发帖间隔(秒)std_post_interval: 发帖间隔标准差(秒)peak_hourly_posts: 峰值小时发帖数peak_daily_posts: 峰值日发帖数
-
情感特征 (2个,基于SnowNLP,只保留平均值):
avg_sentiment_positive: 积极情感平均值avg_sentiment_negative: 消极情感平均值
-
其他特征 (2个):
location_ratio: 包含地理位置的微博比例repost_user_entropy: 转发用户信息熵
模型评估
整体性能
- 准确率 (Accuracy): 98.99%
- 交叉验证F1分数: 0.9851 (±0.0028)
- 测试集F1分数 (宏平均): 0.9898
- 测试集F1分数 (加权平均): 0.9899
各类别性能
人类 (类别0):
- 精确率 (Precision): 0.9847
- 召回率 (Recall): 0.9967
- F1-score: 0.9907
机器人 (类别1):
- 精确率 (Precision): 0.9967
- 召回率 (Recall): 0.9818
- F1-score: 0.9892
注意事项
-
Cookie获取:
- Cookie需要从微博网页版获取
- 访问 https://weibo.com 并登录
- 打开浏览器开发者工具(F12)
- 在Network标签中找到任意请求,复制请求头中的Cookie值
- Cookie格式示例:
SUB=xxx; SUBP=xxx; ... - Cookie用于访问微博API采集用户数据,请妥善保管
-
模型文件:
- 包内已包含训练好的模型文件 (
bot_detection_model.pkl) - 无需额外下载或训练,安装后即可使用
- 包内已包含训练好的模型文件 (
-
数据采集:
- 预测时需要采集用户数据,请确保网络连接正常
- 采集过程可能需要几秒钟时间
-
返回值说明:
label: 预测标签,0表示人类,1表示机器人label_name: 标签名称,'人类' 或 '机器人'bot_score: 机器人得分,是模型预测该账号为机器人的概率值(范围0-1)bot_score接近1表示模型认为该账号很可能是机器人bot_score接近0表示模型认为该账号很可能是人类- 重要提示:
bot_score是模型输出的概率值,不是统计意义上的置信区间或假设检验的p值,应结合具体应用场景理解和使用,避免过度解读
-
模型评估说明:
- 模型评估结果只代表在测试集上的表现,不代表真实情境表现
许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pybotfinder-0.2.2.tar.gz.
File metadata
- Download URL: pybotfinder-0.2.2.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a901f71d4b9005221540fad0b4bb298a9010aca969a124de1f02df1133f9e1d0
|
|
| MD5 |
f60f2b0a315e760bbaba30d5e0d15be9
|
|
| BLAKE2b-256 |
918cb06c1717fd86a8cb17e4dcba0e0a2b02f7021e2633c40b1001ed3ad8ab56
|
File details
Details for the file pybotfinder-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pybotfinder-0.2.2-py3-none-any.whl
- Upload date:
- Size: 1.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0df5961971059a2301bc97000d952f83069de06f6582a3b48979d43447630856
|
|
| MD5 |
937f12be113db859fdc691166207b00c
|
|
| BLAKE2b-256 |
1d1cc807b1c5c4193aa4c9ffe491dfd1ae923008ec34e1bbfe64d8f13d6bcba9
|