'Tweepy pagination KeyError: 0

I tried using tweepy's pagination based on the code provided in it's documentation:

```
import tweepy

auth = tweepy.AppAuthHandler("Consumer Key here", "Consumer Secret here")
api = tweepy.API(auth)

for status in tweepy.Cursor(api.search_tweets, "Tweepy",
                            count=100).items(250):
    print(status.id)
```

However, I get the following error:

```
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16136/3940301818.py in <module>
----> 1 for status in tweepy.Cursor(api.search_tweets, "Tweepy",
      2                             count=100).items(250):
      3     print(status.id)

C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py in __next__(self)
     84 
     85     def __next__(self):
---> 86         return self.next()
     87 
     88     def next(self):

C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py in next(self)
    290         self.page_index += 1
    291         self.num_tweets += 1
--> 292         return self.current_page[self.page_index]
    293 
    294     def prev(self):

KeyError: 0
```

Can someone explain and rectify the error please?



Solution 1:[1]

With the current version of Tweepy 4.8.0, the AuthHandler syntax has changed.

Update Tweepy:

pip install Tweepy -U

and the following should work:

import tweepy

auth = tweepy.OAuth2AppHandler("Consumer Key here", "Consumer Secret here")
api = tweepy.API(auth)

for status in tweepy.Cursor(api.search_tweets, "Tweepy",
                            count=100).items(250):
    print(status.id)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luk?