Search code examples
pythontwittertweepytwitterapi-python

Get tweet url or tweet id using tweepy


So as the question suggests I'm trying to figure out how to get either the tweet id # or the whole url. Using the code below I can get the tweet(s) i want and print them out. However instead of printing them I want to get either the url or at least the tweet id (so that I can make my own url) that I can use in my code later down the line. Any advice? not seeing anything in the tweepy docs about this but perhaps there is something i'm missing or something else I can use to achieve this?

import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("*******", "*****")
auth.set_access_token("******", "*****")
api = tweepy.API(auth)

# Function to extract tweets 
def get_tweets(username): 

        tweets = api.user_timeline(screen_name=username,count = 1) 

        tmp=[]

        tweets_for_csv = [tweet.text for tweet in tweets]
        for t in tweets_for_csv:
            tmp.append(t)
        print(tmp)



if __name__ == '__main__':
    get_tweets("@username")

This code works fine just trying to learn what I would add to get the desired data.


Solution

  • If you have the Tweet ID, you can get to the final URL like this:

    Append it to https://twitter.com/twitter/statuses/ (+ the id)

    Follow the HTTP redirect.

    It sounds like you're not able to get the ID itself - that will be in the tweet.id value for each Tweet - currently you're just saving tweet.text