Error when using Getoldtweets3 filter for a certain period

Asked

Viewed 80 times

0

Hello, I am improving in Python studies and came across the error: Indexerror: list index out of range

the Code I’m running is this:

import GetOldTweets3 as got
max_tweets = 3
tweets=[]
tweetCriteria = got.manager.TweetCriteria().setUsername("@cbv_jlle").setSince("2019-03-29").setUntil("2019-03-31").setMaxTweets(max_tweets)
for i in range(max_tweets):
    tweet = got.manager.TweetManager.getTweets(tweetCriteria)[i]
    tweets.append(tweet.text)  

print(tweets)

Down with the error:

> IndexError                                Traceback (most recent call
> last) <ipython-input-85-287bf080b649> in <module>()
>       5 tweetCriteria = got.manager.TweetCriteria().setUsername("@cbv_jlle").setSince("2019-03-01").setUntil("2019-03-02").setMaxTweets(max_tweets)
>       6 for i in range(max_tweets):
> ----> 7     tweet = got.manager.TweetManager.getTweets(tweetCriteria)[i]
>       8     tweets.append(tweet.text)
>       9 print(tweets)
> 
> IndexError: list index out of range

Some dates work, others don’t. For example if I change the range to setSince("2019-03-29").setUntil("2019-03-31") it works.

1 answer

0


TL;DR

What’s going on is that depending on the period the line tweet = got.manager.TweetManager.getTweets(tweetCriteria) is bringing a number of tweets greater than what you set out to max_tweets and its error was to put this variable to determine the number of iterations for the loop for, when the number in the variable max_tweets is larger than the list of the same you end up trying to access something inaccessible in the object.

Another mistake can be observed in your code, this same line (quoted above) tends to make it extremely slow, because with each passage the whole reading is done again by getTweets(tweetCriteria), I made some changes, try to run this way:

import GetOldTweets3 as got

max_tweets = 3
tweets=[]
tweetCriteria = got.manager.TweetCriteria().setUsername("@cbv_jlle").setSince("2019-03-29").setUntil("2019-03-31").setMaxTweets(max_tweets)

# A leitura dos tweets é feita uma vez só
twts = got.manager.TweetManager.getTweets(tweetCriteria)

for i in range(len(twts)):
    tweet = twts[i]
    tweets.append(tweet.text)  

See working on repl.it.

  • Sidon, you’ve really improved your performance! But even if I increase my max_tweets to more than 100 (for example), it does not return anything to the setSince range("2019-03-01"). setUntil("2019-03-02"). And from what I saw in the source (Firefighters Joinville @cbv_jlle) there should be 50 tweets in total for the 2 days. Does it have to do with the size of the messages, because there are some messages that are a little more extensive?

  • Strange, I did a test here and returns 25 tweets in that period, I’m putting in repl.it, I’ll edit my reply and put the link there.

  • Ok, the link is already in the answer go there and "play" a little, change the variable number max_tweets and the variables determining the period d1 and d2.

  • 1

    Ball show!!! I didn’t know the repl.it!! Thank you so much for your help!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.