为什么用Tweepy开发Twitter Bot
本文逐步介绍如何使用Tweepy开发Twitter bot。Twitter是知名的微博客社交媒体,定位为陌生人社交,能够带来流量。如何在Twitter中获得人气,如何通过Twitter获得准确的流量,如何快速构建自己的Twitter营销系统,本文介绍一个基于Python的Twitter库Tweepy,并通过实例逐步说明如何使用Tweepy开发自己的Twitter Bot。
我们将使用Tweepy开发自己的Twitter bot,整合Twitter的常规操作,做到Set Forget(设置一次即可自动运行)。
Twitter Bot需要具备的功能
- 自动发布:支持文本、图片、视频,支持设置发布间隔,可自动从指定目录获取照片、视频发布。
- 帐户搜索:支持获取指定帐户的following和follower帐户,并支持按条件过滤(following数量、follower数量、活跃度等)。
- 自动follow:可根据检索到的帐户执行自动follow,并设置每次follow的数量。
- 自动unfollow:对于没有follow back的帐户,支持延迟n天后自动unfollow,n天可配置,一天的unfollow数量也可配置。
- 自动retweet:支持按关键字搜索tweet并自动retweet,可配置每天的retweet数量。
可以看到,这个Twitter bot把发布、关注、推送等操作整合在一起,安装好之后即可自动养号;多个账号一起工作,可以用于搭建Twitter营销体系。接下来进入实战。Tweepy是基于Python的高级Twitter库,可从Tweepy、GitHub下载,支持Windows、Linux等系统,我们的Twitter bot将挂载使用,因此下面的实战使用CentOS 8与Python。
第一步:准备一台可访问Twitter的VPS
第一步需要VPS系统来运行Twitter Bot,需要能够访问Twitter(不是中国大陆的机器),并且速度足够快、成本低廉、能够轻松更换IP。推荐Vultr,实施CentOS 8的过程可以搜索相关教程,这里假设我们已经完成了CentOS 8的安装。
第二步:验证是否安装了pip3与python3
运行:
pip3 -V
python3 -V
可以看到默认安装的pip版本为9.0.3、python版本为3.6.8。如果pip和python版本不正确,可以使用以下命令进行升级:
yum install -y python3-pip
第三步:安装Tweepy
运行以下命令:
pip3 install tweepy
第四步:获取访问Twitter账户的密钥
主要包括:
- Consumer key
- Consumer secret
- Access token
- Access secret
密钥的作用在Twitter官方开发人员文档中进行了说明,这里不展开解释,只需要了解连接Twitter帐户、Tweet和Follow即可。那么,在哪里可以拿到密钥?
- 访问Twitter的开发人员主页(https://developer.twitter.com)并单击“Sign Up”,Twitter会提出一系列问题,例如使用程序、申请开发者账户的目的是什么?只要简单回答即可,例如为了学习使用Twitter API等。申请完成后Twitter会进行审核,审核过程非常快,等一会儿即可。
- 回到开发者主页,点击app,选择creative app,在app detail中填写app的名称、图标、URL等即可。重要的是,您可以从Keys and Tokens获得API密钥,与Consumer key和Consumer secret相对应;还有Access token和Access token secret。请确保“Access level(访问级别)”为Read, write, and Direct Messages,如果不是,请转到Permissions页签修改。
注意:请保管好您的账户密钥,有了这四把钥匙,就可以控制你的账户了。
第五步:使用Python和Tweepy开发Bot
经过上述第四阶段的准备工作,我们终于拥有了Bot开发所需的所有材料,从下面开始Bot开发。
测试账户是否正确连接
Python代码:
import tweepy
# Authenticate to Twitter
auth = tweepy.OAuthHandler(“your-consumer-key”, “your-consumer-secret”)
auth.set_access_token(“your-access-token”, “your-access-secret”)
api = tweepy.API(auth)
try:
api.verify_credentials()
print(“Authentication OK”)
except:
print(“Error during authentication”)
然后运行 python3 auth.py,可以看到测试成功。
发布一条文本推文
接下来给出post一个文本消息的Python代码,功能是连接Twitter账号并发一条简单的Tweet,内容是经典的“Hello World!”:
import tweepy
# Authenticate to Twitter
auth = tweepy.OAuthHandler(“your-consumer-key”, “your-consumer-secret”)
auth.set_access_token(“your-access-token”, “your-access-secret”)
# Create API object
api = tweepy.API(auth)
# Create a tweet
api.update_status(“Hello World!”)
定时上传视频并自动发推
最后,扫描目录下的视频,每隔指定的时间(例如1小时)发送一次视频,传输完成后,将传输的文件移动到其他目录。在这种情况下,首先必须使用名为API的对象的media_upload方法上传视频,必须将媒体类型指定为视频(media_category),然后使用API对象的update_status方法发送tweet,并将media_ids指定为刚刚上传的视频media_id。tweet的文本可以为空:
import tweepy
import os
import time
import shutil
source_dir = ‘/tweepy/video’
target_dir = ‘/tweepy/done/’
check_after_secs = 3600
tweet = ”
# Authenticate to Twitter
auth = tweepy.OAuthHandler(“your-consumer-key”, “your-consumer-secret”)
auth.set_access_token(“your-access-token”, “your-access-secret”)
# Create API object
api = tweepy.API(auth)
try:
for root, sub_dirs, files in os.walk(source_dir):
for special_file in files:
spcial_file_dir = os.path.join(root, special_file)
print(‘Processing video %s’ % spcial_file_dir)
media = api.media_upload(filename=spcial_file_dir,media_category=”tweet_video”)
post_result = api.update_status(status=tweet,media_ids=[media.media_id])
# Move uploaded file
shutil.move(spcial_file_dir, target_dir)
print(‘Checking after %s seconds’ % str(check_after_secs))
time.sleep(check_after_secs)
except:
print(“Error during read dir”)
以上这是使用Tweepy开发Twitter bot的全过程。

评论(0)