Search code examples
pythontwittertwitter-oauthtweepy

How to filter unwanted attributes from Twitter timeline using Tweepy


The below code gives me all information about each status message.

I would like only the text message and the corresponding name of the person.

How do I filter out the attributes that I do not require?

for status in tweepy.Cursor(api.home_timeline).items(3):
    print(json.dumps(status._json, indent=4))

Solution

  • If you look at Twitter documentation relative to the home_timeline, you can see the full structure that api returns.

    Since api returns a list of dictionaries, you can easily filter each tweet in this way

    for tweet in tweepy.Cursor(api.home_timeline).items(3):
        print(tweet.text)
        print(tweet.user.name)