我将逐步教你如何使用Tweepy开发Twitter bot,Twitter是世界上最有名的微博社交媒体,定位是陌生人社交网站,可以获得大量流量。如何在Twitter中获得人气,如何通过Twitter获得准确的流量,如何快速构建自己的Twitter营销系统,本文介绍了一个名为Tweepy的基于Python的Twitter库,并通过实例逐步说明了如何使用Tweepy开发自己的Twitter Bot。

逐步教你如何使用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的营销体系。接下来,我们开始实战。特别是基于Python的顶级Twitter库,可以从Tweepy、GitHub下载,支持Window、Linux等系统,我们的Twitter bot将挂机使用,所以下面的实战将使用Centos 8 Python。

第一步需要VPS系统来运行Twitter Bot,需要能够访问Twitter(不是中国大陆的机器),并且速度足够快、成本低廉、能够轻松更换IP。推荐Vultr,实施CentOS8的过程可以百度搜索相关教程,这里假设我们已经完成了CentOS8的安装。

逐步教你如何使用Tweepy开发Twitter bot-国外网赚博客

在第二步中,验证是否安装了pip3、python3

运行:pip3 -V

python 3-v

逐步教你如何使用Tweepy开发Twitter bot-国外网赚博客

看到默认安装的pip版本为9.0.3、python版本3.6.8。如果pip和python版本不正确,可以使用以下命令进行升级:

Yum install -y python3-pip

第三步是安装Tweepy

运行以下命令:

Pip3 install tweepy

逐步教你如何使用Tweepy开发Twitter bot-国外网赚博客

第四步是获取访问推特账户的密钥

主要包括:

  1. Consumer key
  2. Consumer secret
  3. Access token
  4. Access secret

密钥的作用在Twitter官方开发人员文档中进行了说明,这里不解释,只需要了解连接Twitter帐户、Tweet和Follow即可。那么,在哪里可以拿到密钥?

  1. 当您访问twitter的开发人员主页(https://developer.twitter.com)并单击“Sign Up”时,Twitter将提出一系列问题,例如使用程序,申请开发者账户的目的是什么?只要简单的回答就行了。例如,为了学习使用Twitter API等,申请完成后Twitter会进行审核,审核过程非常快,等一会儿就可以了。
  2. 回到开发者主页,点击app,选择creative app,在app detail中填写app的名称、图标、URL等即可。重要的是,您可以从Keys and Tokens获得API密钥、API。与Consumer key和Consumer secret相对应;还有Access token和Access token secret。请确保“Access level(访问级别)”为Read, write, and Direct Messages,如果不是,请转到Permissions页签修改。

逐步教你如何使用Tweepy开发Twitter bot-国外网赚博客

注意:请保管好您的账户密钥,有了这四把钥匙,就可以控制你的账户了。

第五步是使用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

逐步教你如何使用Tweepy开发Twitter bot-国外网赚博客

可以看到测试成功。

接下来,我们给出 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的全过程。