问题
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