Python: How to only loop over first 'x' items of a very long list; Writing a Twitter bot with tweepy

二次信任 提交于 2019-12-12 06:47:57

问题


Noob here. I'm sure this is a simple problem. I'm writing a Twitter bot with tweepy. I've hit a snag. I'm getting a 'Rate limit exceeded' error when I'm calling the api to make a list of followers. From what I understand, this is due to Twitter imposing a limit on calls to the api.

My code snippet:

for follower in tweepy.Cursor(api.followers).items():
    follower_ids.append(follower.id)

My question: How do I grab the first 'x' items from tweepy.Cursor(api.followers).items()? in range seemed like the obvious solution, but I couldn't get it to work.


回答1:


You can restrict tweepy's cursor limit like this:

# Only iterate through the first 200 followers
for follower in Cursor(api.followers).limit(200):
     follower_ids.append(follower.id)

This is pretty explicit in the docs, see here.



来源:https://stackoverflow.com/questions/20200532/python-how-to-only-loop-over-first-x-items-of-a-very-long-list-writing-a-twi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!