【问题标题】:Tweepy pagination KeyError: 0Tweepy 分页 KeyError: 0
【发布时间】:2022-04-27 00:09:58
【问题描述】:

我根据文档中提供的代码尝试使用 tweepy 的分页:

```
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)
```

但是,我收到以下错误:

```
---------------------------------------------------------------------------
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
```

有人可以解释并纠正错误吗?

【问题讨论】:

  • 我无法重现这一点。您的代码与示例完全相同吗?

标签: pagination tweepy


【解决方案1】:

使用当前版本的 Tweepy 4.8.0,AuthHandler 语法发生了变化。

更新 Tweepy:

pip install Tweepy -U

以下应该可以工作:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2022-07-25
    相关资源
    最近更新 更多