25

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?

user8028
  • 463
  • 2
  • 6
  • 9
  • 2
    You would have to access the attributes of the objects. Example `status.text`. Here are the attributes - https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline – karthikr Aug 31 '14 at 01:49
  • 1
    the above link is dead. Try [https://dev.twitter.com/overview/api/tweets](https://dev.twitter.com/overview/api/tweets) – Shawn Mehan Jun 15 '17 at 22:17

3 Answers3

26

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
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • So i would simply put `print dir(stuff)` ? – user8028 Aug 31 '14 at 01:52
  • @user8028 `for status in stuff: print dir(status)`. – alecxe Aug 31 '14 at 01:53
  • So could I do a similar loop to print the `text` attribute for example `for status in stuff: print status.text` or any other attribute listed by dir()? – user8028 Aug 31 '14 at 01:59
  • @user8028 yup, use the dot notation for every attribute you need from every `status`. – alecxe Aug 31 '14 at 02:00
  • this may be slightly off topic for the question but still along the lines of attributes, but can you directly access hashtags used as an attribute or is it necessary to parse the text attribute? – user8028 Aug 31 '14 at 02:38
5
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

user140536
  • 144
  • 1
  • 7
1

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.

Pawel Kam
  • 1,684
  • 3
  • 14
  • 30