Here I am trying to use tweepy.Paginator to search over 100+ tweets and then load them into pandas df for further analysis. I, however, keep getting the repeated wrong username in each row, instead of the username of the author of the tweet in my df output.
Below is an example of my code. Please can anyone help fix it?
# create a list of records
query = 'Wizkid'
tweet_info_ls = []
# iterate over each tweet and corresponding user details
for tweet in tweepy.Paginator(client.search_recent_tweets,query=query,tweet_fields=['context_annotations', 'created_at','author_id', 'public_metrics'],
expansions=['author_id','referenced_tweets.id'], max_results=100, user_fields=['username', 'name','public_metrics']).flatten(limit = 100):
tweet_info = {
'created_at': tweet.created_at,
'text': tweet.text,
#'source': tweet.source,
'name': user.name,
'username': user.username,
#'location': user.location,
#'verified': user.verified,
#'description': user.description,
'followers': user.public_metrics['followers_count'],
'repost':tweet.public_metrics['retweet_count']
}
tweet_info_ls.append(tweet_info)
# create dataframe from the extracted records
tweets_df = pd.DataFrame(tweet_info_ls)
# display the dataframe
tweets_df.head(20)
and here's the head of the output