Understand Tweepy’s Tweets JSON Response (Twitter API)

APIs often return responses in the format of a JSON file. In this tutorial, you will learn how to understand and use Twitter API’s response.

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:

Join the 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

    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, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
    

    You can now run your first Twitter API call.

    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
    

    Show a Tweet’s JSON Response

    # Select first Tweet from the Above response.
    tweet = tweets[0]
    #or
    #tweet = tweets_extended[0]
    
    # Show as a JSON
    tweet._json
    
    # show attributes of the JSON that you can select.
    list(tweet._json.keys())
    

    Parse Tweet’s Json Response

    Here we will parse the Tweet’s JSON response and print some data.

    # Get Tweet data
    print(f'Created at:{tweet.created_at}')
    print(f'Tweet id:{tweet.id}')
    
    # Get tweet text
    try:
        print(f'Tweet text:{tweet.text}')
    except:
        # Get tweet text in extended mode
        print(f'Tweet text:{tweet.full_text}')
    
    # Get tweet status
    print(f'Is Retweeted:{tweet.retweeted}')
    print(f'Is favorited:{tweet.favorited}')
    print(f'Is quote:{tweet.is_quote_status}')
    
    # Get Tweet statistics
    print(f'Retweet count:{tweet.retweet_count}')
    print(f'Favorite count:{tweet.favorite_count}')
    
    # Get tweet lang
    print(f'Tweet lang:{tweet.lang}')
    
    # Get replied to
    print(f'In reply to status id:{tweet.in_reply_to_status_id}')
    print(f'In reply to user id:{tweet.in_reply_to_user_id}')
    

    Show Entities of a Tweet

    You can also extract entities like Hashtags, User Mentions and URLs from a Tweet.

    tweet = tweets_extended[0]
    print(tweet.entities['hashtags'])
    for i in tweet.entities:
        print(f'{i} type is {type(tweet.entities[i])}')
        if not tweet.entities[i]:
            print(f'There are no {i}')
        else:
            for j in tweet.entities[i]:
                print(f'{i} entities are {j}')
                for k,v in j.items():
                    print(f'{i}:{k} is {v}')
        print('\n')
    

    Conclusion

    In this tutorial, you learned how to parse the Twitter API’s JSON response with Python.

    5/5 - (2 votes)