I could successfully get the data of a user in Python by get_all_tweets(user_name)
. Now I would like to retrieve data from several users in the same code.
I tried to copy the same code and append it to the code. Instead of user_name
I used user_name1
, I also tried to change all the other variables to ...1
. The list for user_name is still produced. The list for user_name1
not.
import tweepy
import pandas as pd
import webbrowser
import time
def get_all_tweets(user_name):
bearer_token ='XXX'
client = tweepy.Client(bearer_token=bearer_token)
user = client.get_user(username = user_name)
user_id = user.data.id
userTweets = []
count = 0
limit = 3000
pagination_token = None
start_time = '2023-03-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
end_time = '2023-03-02T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
while count < limit:
tweets = client.get_users_tweets(
id = user_id,
exclude = ['retweets','replies'],
tweet_fields = ['created_at','public_metrics'],
pagination_token = pagination_token,
start_time = start_time,
end_time = end_time,
max_results = 100)
if 'next_token' in tweets.meta.keys():
if (tweets.meta['next_token']) is not None:
pagination_token = tweets.meta['next_token']
else:
pagination_token = None
else:
pagination_token = None
if tweets.data is not None:
for tweet in tweets.data:
count += 1
userTweets.append({
"id": tweet.id,
"test": tweet.text,
"date": tweet.created_at,
"public":tweet.public_metrics
})
if pagination_token is None:
break
return userTweets, count
# user_name can assign your tweet name
user_name = 'Weltwoche'
[userTweets, count] = get_all_tweets(user_name)
def get_all_tweets(user_name1):
bearer_token ='XXX'
client = tweepy.Client(bearer_token=bearer_token)
user = client.get_user(username = user_name1)
user_id = user.data.id
userTweets1 = []
count1 = 0
limit = 3000
pagination_token = None
start_time = '2023-03-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
end_time = '2023-03-02T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
while count1 < limit:
tweets1 = client.get_users_tweets(
id = user_id,
exclude = ['retweets','replies'],
tweet_fields = ['created_at','public_metrics'],
pagination_token = pagination_token,
start_time = start_time,
end_time = end_time,
max_results = 100)
if 'next_token' in tweets1.meta.keys():
if (tweets1.meta['next_token']) is not None:
pagination_token = tweets1.meta['next_token']
else:
pagination_token = None
else:
pagination_token = None
if tweets1.data is not None:
for tweet1 in tweets1.data:
count1 += 1
userTweets1.append({
"id": tweet.id,
"test": tweet.text,
"date": tweet.created_at,
"public":tweet.public_metrics
})
if pagination_token is None:
break
return userTweets1, count1
# user_name can assign your tweet name
user_name1 = 'Blick'
[userTweets1, count1] = get_all_tweets(user_name1)
print(count)
for tweet in userTweets:
print(tweet)
print(count1)
for tweet1 in userTweets1:
print(tweet1)
Using user array and call same function.
import tweepy
def get_all_tweets(user_name):
bearer_token ='your bearer token' # From https://developer.twitter.com/en/portal
client = tweepy.Client(bearer_token=bearer_token)
user = client.get_user(username = user_name)
user_id = user.data.id
userTweets = []
count = 0
limit = 3000
pagination_token = None
start_time = '2023-01-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
end_time = '2023-01-31T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
while count < limit:
tweets = client.get_users_tweets(
id = user_id,
exclude = ['retweets','replies'],
pagination_token = pagination_token,
start_time = start_time,
end_time = end_time,
max_results = 100)
if 'next_token' in tweets.meta.keys():
if (tweets.meta['next_token']) is not None:
pagination_token = tweets.meta['next_token']
else:
pagination_token = None
else:
pagination_token = None
if tweets.data is not None:
for tweet in tweets.data:
count += 1
userTweets.append({
"id": tweet.id,
"test": tweet.text
})
if pagination_token is None:
break
return userTweets, count
# you can make users array in here
user_names = ["elonmusk", "BarackObama"]
for user_name in user_names:
[tweets, count] = get_all_tweets(user_name)
for tweet in tweets:
tweet_url = f"https://twitter.com/{user_name}/status/{tweet['id']}"
print(tweet)
print(tweet_url)
print(user_name + "'s tweets count is " + str(count))
Result
elonmusk's tweets count is 78
...
BarackObama's tweets count is 26