'Return a users tweets with tweepy

I am using tweepy and python 2.7.6 to return the tweets of a specified user

My code looks like:

import tweepy

ckey = 'myckey'
csecret = 'mycsecret'
atoken = 'myatoken'
asecret = 'myasecret'



auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

api = tweepy.API(auth)

stuff = api.user_timeline(screen_name = 'danieltosh', count = 100, include_rts = True)

print stuff

However this yields a set of messages which look like<tweepy.models.Status object at 0x7ff2ca3c1050>

Is it possible to print out useful information from these objects? where can I find all of their attributes?



Solution 1:[1]

Unfortunately, Status model is not really well documented in the tweepy docs.

user_timeline() method returns a list of Status object instances. You can explore the available properties and methods using dir(), or look at the actual implementation.

For example, from the source code you can see that there are author, user and other attributes:

for status in stuff:
    print status.author, status.user

Or, you can print out the _json attribute value which contains the actual response of an API call:

for status in stuff:
    print status._json

Solution 2:[2]

import tweepy
import tkinter

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# set parser=tweepy.parsers.JSONParser() if you want a nice printed json response.

userID = "userid"
user = api.get_user(userID)

tweets = api.user_timeline(screen_name=userID, 
                           # 200 is the maximum allowed count
                           count=200,
                           include_rts = False,
                           # Necessary to keep full_text 
                           # otherwise only the first 140 words are extracted
                           tweet_mode = 'extended'
                           )

for info in tweets[:3]:
    print("ID: {}".format(info.id))
    print(info.created_at)
    print(info.full_text)
    print("\n")

Credit to https://fairyonice.github.io/extract-someones-tweet-using-tweepy.html

Solution 3:[3]

In Tweeter API v2 getting tweets of a specified user is fairly easy, provided that you won’t exceed the limit of 3200 tweets. See documentation for more info.

import tweepy

# create client object
tweepy.Client(
        bearer_token=TWITTER_BEARER_TOKEN,
        consumer_key=TWITTER_API_KEY,
        consumer_secret=TWITTER_API_KEY_SECRET,
        access_token=TWITTER_ACCESS_TOKEN,
        access_token_secret=TWITTER_TOKEN_SECRET,
    )

# retrieve first n=`max_results` tweets
tweets = client.get_users_tweets(id=user_id, **kwargs)
# retrieve using pagination until no tweets left
while True:
    if not tweets.data:
        break
    tweets_list.extend(tweets.data)
    if not tweets.meta.get('next_token'):
        break
    tweets = client.get_users_tweets(
        id=user_id,
        pagination_token=tweets.meta['next_token'],
        **kwargs,
    )

The tweets_list is going to be a list of tweepy.tweet.Tweet objects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 user140536
Solution 3 Pawel Kam