How To Create Twitter Bot using Python And Tweepy
The
goal of this tutorial is to get you write your own Twitter bot using Python and tweepy. We will get tweets based on Hashtag and Geo Graphical Area. First of all, We need to wrapper for python script to access Twitter account.
Read : How To Create Twitter App In 4 Easy Steps
Note Keys and Tokens we will use it in script. Next, we need to install tweepy. Tweepy is Python library for accessing the Twitter API. Tweepy will be used to access the twitter API with Python.
1
|
pip install tweepy
|
Now, Its time to write python script. Create a new file with .py
extension. Eg. twitterApp.py
Import tweepy and sleep library
Import tweepy
to access twitter api and sleep
to put time delay in script.
1
2
|
import tweepy
from time import sleep
|
Define Consumer key, Secret and Tokens
1
2
3
4
|
consumerKey = '<Consumer Key>'
consumerSecret = '<Consumer Secret>'
accessToken = '<Access Token>'
accessTokenSecret = '<Access Token Secret>'
|
Initialise Tweepy
Create object auth
from tweepy library and set access token. method set_access_token()
will be used to set access token.
1
2
3
|
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
|
Getting Tweets
Listing out tweets between 20th November 2016 to 03rd December, tweeted from silicon velly, having hashtag #php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
for tweet in tweepy.Cursor(api.search, q='#php', since='2016-11-20', until='2016-12-03', geocode='37.3875,122.0575,5000km', lang='en').items(10000):
try:
# Print User Name who tweeted
print('\nTweet by: @' + tweet.user.screen_name)
sleep(5)
# Retweet the tweet
tweet.retweet()
print('Retweeted the tweet')
# Favorite the tweet
tweet.favorite()
print('Favorited the tweet')
# Follow the user who tweeted
if not tweet.user.following:
tweet.user.follow()
print('Followed the user')
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
|
geocode='37.3875,122.0575,5000km'
: Returns tweets based on latitude and longitude nearby 5000 kilo meters.
q='#php'
: Hashtag whose tweets we want to use.
tweet.user.screen_name
: Returns User Name of owner
tweet.favorite()
: This function used to retweet the tweet
tweet.favorite()
: Used to favorite the tweet
tweet.user.follow()
: To follow the user
lang='en'
: Returns tweets having language English only
items(10000)
: Returns 10000 maximum tweets
Full Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import tweepy
from time import sleep
consumerKey = '<Consumer Key>'
consumerSecret = '<Consumer Secret>'
accessToken = '<Access Token>'
accessTokenSecret = '<Access Token Secret>'
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search, q='#php', since='2016-11-20', until='2016-12-03', geocode='37.3875,122.0575,5000km', lang='en').items(10000):
try:
# Print User Name who tweeted
print('\nTweet by: @' + tweet.user.screen_name)
sleep(5)
# Retweet the tweet
tweet.retweet()
print('Retweeted the tweet')
# Favorite the tweet
tweet.favorite()
print('Favorited the tweet')
# Follow the user who tweeted
if not tweet.user.following:
tweet.user.follow()
print('Followed the user')
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
|
Script is now ready. Now, Run script using following command to set it loose in the world.
1
|
python twitterApp.py
|