-1

I want to download tweets of two users at the same time. So far I' ve downloaded tweets of one user. Here is some code.

 tweetsL = []
try:
  user_timeline = twitter.get_user_timeline(screen_name= 'PrimeministerGR', count=100, tweet_mode = 'extended')
except:
  print("Error getting tweets:")

  print(len(user_timeline) , "tweets")
# We add the text of the tweet in the list we create
for tweet in user_timeline:
    tweetsL.append(tweet)
Harmon758
  • 5,084
  • 3
  • 22
  • 39

1 Answers1

0

You could just access the same API endpoint multiple times for all users you are interested in. Also, instead of iterating over the tweets and appending them one by one, you can use the method extend, which works just like append, but expects a list of items to be added to the original list.

tweetsL = []
users = ['PrimeministerGR', 'user2', 'user3']
try:
    for user in users:
        user_timeline = twitter.get_user_timeline(screen_name=user, count=100, tweet_mode='extended')

        print(len(user_timeline), "tweets")
        tweetsL.extend(user_timeline)
except:
    print("Error getting tweets:")

EDIT: To process the tweets in tweetsL e.g. print them, just use another loop similar to looping over the users:

for tweet in tweetsL:
    print(tweet)
    # you can do whatever with the tweets here
schilli
  • 1,700
  • 1
  • 9
  • 17