如何在Python中制作 Twitter 机器人?
Twitter 是美国的微博和社交网络服务,用户在该服务上发布消息并与被称为“推文”的消息进行交互。在本文中,我们将使用Python制作一个 Twitter Bot 。
Python和 Javascript 可用于开发自动 Twitter 机器人,它可以自行完成许多任务,例如:
- 转推带有特定#hastags的推文。
- 收藏/喜欢带有特定#hashtags的推文。
- 关注使用特定#hashtags发推文的用户。
- 如果获得许可,也可以DM 用户。
要求
安装 Tweepy
为此,我们需要一个名为Tweepy的Python库来访问Twitter API 。我们可以通过三种方式安装 tweepy:
1. 使用 pip 命令
$ pip install tweepy
2.克隆tweepy的GitHub仓库
$ git clone https://github.com/tweepy/tweepy.git
$ cd tweepy
$ pip install
3.直接克隆仓库
$ pip install git+https://github.com/tweepy/tweepy.git
注册 Twitter 开发者帐户
- 为您的 Twitter Bot 注册一个单独的帐户,然后通过此链接申请 Twitter 开发人员帐户 https://developer.twitter.com/en/apply-for-access
- 输入必要的详细信息并等待您的邮件确认。确认后,单击创建应用程序选项。
- 输入必要的详细信息以生成密钥和访问令牌。
- 复制密钥并妥善保管。
开发 Twitter 机器人
创建一个文件twitterbot_retweet.py并粘贴以下代码。
Python3
import tweepy
from time import sleep
from credentials import *
from config import QUERY, FOLLOW, LIKE, SLEEP_TIME
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print("Twitter bot which retweets, like tweets and follow users")
print("Bot Settings")
print("Like Tweets :", LIKE)
print("Follow users :", FOLLOW)
for tweet in tweepy.Cursor(api.search, q = QUERY).items():
try:
print('\nTweet by: @' + tweet.user.screen_name)
tweet.retweet()
print('Retweeted the tweet')
# Favorite the tweet
if LIKE:
tweet.favorite()
print('Favorited the tweet')
# Follow the user who tweeted
# check that bot is not already following the user
if FOLLOW:
if not tweet.user.following:
tweet.user.follow()
print('Followed the user')
sleep(SLEEP_TIME)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
Python3
# Edit this config.py file as you like
# This is hastag which Twitter bot will
# search and retweet You can edit this with
# any hastag .For example : '# javascript'
QUERY = '# anything'
# Twitter bot setting for liking Tweets
LIKE = True
# Twitter bot setting for following user who tweeted
FOLLOW = True
# Twitter bot sleep time settings in seconds.
# For example SLEEP_TIME = 300 means 5 minutes.
# Please, use large delay if you are running bot
# all the time so that your account does not
# get banned.
SLEEP_TIME = 300
Python3
# This is just a sample file. You need to
# edit this file. You need to get these
# details from your Twitter app settings.
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
现在制作另一个文件来指定你的机器人应该做什么。将其命名为config.py
编辑 #hashtag根据您的选择等或跟随选项True 或 False 。
Python3
# Edit this config.py file as you like
# This is hastag which Twitter bot will
# search and retweet You can edit this with
# any hastag .For example : '# javascript'
QUERY = '# anything'
# Twitter bot setting for liking Tweets
LIKE = True
# Twitter bot setting for following user who tweeted
FOLLOW = True
# Twitter bot sleep time settings in seconds.
# For example SLEEP_TIME = 300 means 5 minutes.
# Please, use large delay if you are running bot
# all the time so that your account does not
# get banned.
SLEEP_TIME = 300
接下来创建一个文件credentials.py并将您的访问令牌小心地粘贴在单引号 ' ' 之间。
Python3
# This is just a sample file. You need to
# edit this file. You need to get these
# details from your Twitter app settings.
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
部署
使用此命令从命令提示符/终端运行twitterbot_retweet.py文件。
$ python twitterbot_retweet.py
它有效!