Tweepy Basics Functions for Twitter API (Python)

In this tutorial, we will learn the basic functions that you can use to interact with the Twitter API with Python.

This tutorial is part of the complete guide on the Twitter API with Python.

Getting Started

Before you can use the Twitter API, you will need:


Subscribe to my Newsletter


  1. Python Installed. Read How to install Python with Anaconda
  2. Apply for a Twitter Developer Account
  3. Twitter API Credentials (API Keys)
    • api_key
    • api_secret
    • access_token
    • access_secret

Install Tweepy

$ pip install tweepy

Authenticate with Tweepy

To authenticate to the Twitter API with Tweepy, use this function.

import tweepy

api_key = "..."
api_secrets = "..."
access_token = "..."
access_secret = "..."

# Authenticate to Twitter
auth = tweepy.OAuthHandler(api_key,api_secrets)
auth.set_access_token(access_token,access_secret)

api = tweepy.API(auth)

try:
    api.verify_credentials()
    print('Authentication OK')
except:
    print('Error during authentication')

You can now run your first Twitter API call.

Getting User Information

Get User

The first step is to call the API to get the user that you want to get information from.

The get_user() method takes a Twitter screen_name as a parameter.

user = api.get_user(screen_name='ChouinardJC')

Get the Twitter User Information

You can look at the data that you can extract for a given user using:

user._json

Here are some examples:

print(f"user.name: {user.name}")
print(f"user.screen_name: {user.screen_name}")
print(f"user.location: {user.location}")
print(f"user.description: {user.description}")
print(f"user.followers_count: {user.followers_count}")
print(f"user.listed_count: {user.listed_count}")
print(f"user.statuses_count: {user.statuses_count}")
print(f"user urls: {user.entities['url']['urls'][0]['expanded_url']}")

Print Latest Followers of a User

Here we will extract the latest followers of a specific user.

print('Latest Followers:')
for follower in user.followers():
    print('Name: ' + str(follower.name))
    print('Username: ' + str(follower.screen_name))

It might not be super useful in that format, but don’t worry, we will convert that later.

L’attribut alt de cette image est vide, son nom de fichier est image-22.png.

Update Your Profile Information

Update status

description = 'Sr SEO Specialist at Seek and a #Python for #SEO advocate'
api.update_profile(description=description)

Update Status and Image.

image = 'ChouinardJC.jpg'
description = 'Sr SEO Specialist at Seek and a #Python for #SEO advocate'
api.update_with_media(image, description) # Update media an send a Tweet.

Interacting With Users

Follow a User

api.create_friendship('ChouinardJC')

Unfollow a User

api.destroy_friendship('KimKardashian')

Get All Followers of a User into a DataFrame

import pandas as pd

### Authentication skipped for simplicity. See Step 1 ###

def get_followers(api,user_name):
    print(f'Getting followers from {user_name}')
    followers = []
    for page in tweepy.Cursor(api.followers, screen_name=user_name, wait_on_rate_limit=True,count=200).pages():
        try:
            followers.extend(page)
        except tweepy.TweepError as e:
            print(f'Waiting {60}s for limit:', e)
            time.sleep(60)
    return followers

def followers_to_df(user_name, data):
    print('Adding Followers to Dataframe')
    columns = ["name", "screen_name", "description", "followers_count", "followers_count",
               'friends_count', "listed_count", "favourites_count", "created_at"]
    simple_list = [[]]
    for profile_data in data:
        profile = []
        for header in columns:
            profile.append(profile_data._json[header])
        simple_list.append(profile)
    df = pd.DataFrame(simple_list,columns=columns)
    return df

followers = get_followers(api,user)
df = followers_to_df(user, followers)
df

Interact With Twitter Lists

When you find a twitter list that you like, you can find the list id in the URL.

Follow All Users In a List

import pandas as pd

### Authentication skipped for simplicity. See Step 1 ###

def get_members(api,list_id):
    member_screen_names = []
    for pages in tweepy.Cursor(api.list_members,list_id=list_id).pages():
        for page in pages:
            member_screen_names.append(page._json['screen_name'])
    return member_screen_names

def add_users(api,screen_name,list_id):
    try:
        api.add_list_member(screen_name=screen_name, list_id=list_id)
    except:
        print(f'error: {screen_name}')

def remove_users(api,screen_name,list_id):
    try:
        api.remove_list_member(screen_name=screen_name, list_id=list_id)
    except:
        print(f'error: {screen_name}')

def friending_everyone_in_list(member_screen_names):
    for m in member_screen_names:
        # creating the friendship 
        print(f'Friending {m}')
        api.create_friendship(m) 

def unfriending_everyone_in_list(member_screen_names):
    for m in member_screen_names:
        print(f'Unfriending {m}')
        api.destroy_friendship(m)
        

member_screen_names = get_members(api,list_id)
friending_everyone_in_list(member_screen_names)
#unfriending_everyone_in_list(member_screen_names)

Get Marketing Data From all User in a Twitter List

Here we are going to export a CSV with data from all the users in a list.

import pandas as pd

### Authentication skipped for simplicity. See Step 1 ###

def get_members(api,list_id):
    member_screen_names = []
    for pages in tweepy.Cursor(api.list_members,list_id=list_id).pages():
        for page in pages:
            member_screen_names.append(page._json['screen_name'])
    return member_screen_names

def add_users(api,screen_name,list_id):
    try:
        api.add_list_member(screen_name=screen_name, list_id=list_id)
    except:
        print(f'error: {screen_name}')

def remove_users(api,screen_name,list_id):
    try:
        api.remove_list_member(screen_name=screen_name, list_id=list_id)
    except:
        print(f'error: {screen_name}')

data = get_members(api,list_id)

ulist=[]

for u in data:
    print(u)
    user = api.get_user(u)
    try:
        url = user.entities['url']['urls'][0]['expanded_url']
    except:
        url = ''
    udata = [user.name,user.screen_name,user.location,user.description,user.followers_count,user.listed_count,user.statuses_count,url]
    ulist.append(udata)

df = pd.DataFrame(ulist,columns=['name','screen_name','location','description','followers_count','listed_count','statuses_count','url'])
df.sort_values(by='followers_count',ascending=False).head(10)
# df.to_csv('filename.csv',index=False)

Get Tweets Data

Get 200 Tweets from a User Timeline

### Authentication skipped for simplicity. See Step 1 ###

tweets = api.user_timeline(id='ChouinardJC', count=200)
tweets_extended = api.user_timeline(id='ChouinardJC', tweet_mode='extended', count=200)

# Print number of tweets
print(len(tweets))
#200

Tweet Data to a DataFrame

Now, let’s create a DataFrame with the data from the user timeline’s tweets.

to_extract = [
    'id',
    'created_at',
    'text',
    'full_text',
    'retweeted',
    'favorited',
    'is_quote_status',
    'retweet_count',
    'favorite_count',
    'lang',
    'in_reply_to_status_id',
    'in_reply_to_user_id'
    ]

tweet_entities = [
    ('hashtags','text'),
    ('user_mentions','screen_name'),
    ('urls','expanded_url')
]
tweets_data = []

for tweet in tweets_extended:
    tweet = tweet._json
    tweet_data = {}
    for e in to_extract:
        try:
            tweet_data[e]=tweet[e]
        except:
            continue
    for entity in tweet_entities:
        entity_list = []
        for t in tweet['entities'][entity[0]]:
            entity_list.append(t[entity[1]])
        tweet_data[entity[0]] = entity_list

    tweets_data.append(tweet_data)
    # Get Tweet data
df = pd.DataFrame(tweets_data)

Like and Retweet Posts

Like, Follow and Retweet Every Post Mentioning You

tweets = api.mentions_timeline(count=10) # Limit to the latest 10
for tweet in tweets:
    try:
        tweet.favorite()
    except:
        pass
    try:
        tweet.user.follow()
    except:
        pass
    try:
        if tweet.in_reply_to_status_id_str == None: # Exclude Replies to limit Spam.
            tweet.retweet()
    except:
        pass

Like Every Tweet in the Last 24 Hours From a List

import tweepy
import datetime
from datetime import timedelta

consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_secret = '...'
list_id = '...' # From the URL of the list

# Authenticate to Twitter
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)


api = tweepy.API(auth)

try:
    api.verify_credentials()
    print('Authentication OK')
except:
    print('Error during authentication')


python_seo_list = set()
# Iterate through all members of the owner's list
for member in tweepy.Cursor(api.list_members, list_id=list_id).items():
    python_seo_list.add(member.screen_name)


delta = timedelta(hours=24)
endDate =   datetime.datetime.now()
startDate = endDate - delta

for i in python_seo_list:

    tweets = []
    tmpTweets = api.user_timeline(i)
    for tweet in tmpTweets:
        if tweet.created_at < endDate and tweet.created_at > startDate:
            tweets.append(tweet)

    for tweet in tweets:
        try:
            if (tweet.in_reply_to_status_id_str == None) and (not tweet.retweeted) and ('RT @' not in tweet.text):
                tweet.favorite()
        except:
            pass

Debugging

If you get the following error, that means that you don’t have enough access level to the API, which means that you should follow this URL and complete the right access level.

Forbidden: 403 Forbidden
453 - You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

And still, you may have to pay for further access:

Conclusion

There are many more things that can be done with the Twitter API, but I hope the above helped you become a better Twitter API user.

5/5 - (1 vote)