I want to store the IDs of some tweets using tweepy

Viewed 49

This code prints the IDs but also raises a TypeError

for tweet in client.search_recent_tweets(search_string):
    for tweet_id in tweet:
        print(tweet_id['id'])

Simply printing Tweet gives the following data

Response(data=[<Tweet id=#ID text='#text'>], includes={}, errors=[], meta={'newest_id': '#ID of first tweet', 'oldest_id': '#ID of last tweet', 'result_count': 10, 'next_token': '#Token no.'})

I basically want to extract the Tweet IDs

2 Answers

I don't understand how your double loop is supposed to work.

Anyway, you can see that the tweets are in the response.data, so simply iterate through it:

response = client.search_recent_tweets(search_string)  # Get the API response

tweets = response.data                                 # Tweets are the data

for tweet in tweets:                                   # Iterate through the tweets
    print(tweet.id)                                    # You can now access their id
Related