Search code examples
pythontimetwittertweetstwython

How can I set a limit to the tweets I want to download?


I downloaded the tweets I want but my code is running infinity. The Google Collab environment said that I cannot interrupt the code. How can I stop the code from running without canceling it? Here is my code: (ragkousis is a greek politician)

while len(ragkousis) != 0:
try:
  ragkousis = twitter.get_user_timeline(screen_name= 'gragkousis',  count=100, tweet_mode = 'extended', exclude_replies = 'true', exclude_retweets = 'true')
except:                                              
    print("Error getting tweets:")
if len(ragkousis) > 0:
    print("I Got:", len(ragkousis), " tweets more... Last ID:", ragkousis[len(ragkousis)-1]['id']-1)
for tweet in ragkousis and voridis:
    tweetsL.append(tweet)

print(len(tweetsL), 'tweets')


Solution

  • If you want to set limit based on length of the list tweetL, simply add the following inside the while loop:

    if len(tweetL) > 1000:
        break
    

    You can replace the value 1000 with the limit of your choice.