python - 如何获得大量关注者 Tweepy

标签 python twitter tweepy

我正在尝试使用 Tweepy 从一个拥有大约 50 万粉丝的帐户中获取完整的粉丝列表,并且我有一个代码可以为我提供较小帐户的用户名,例如 100 个以下,但如果我得到一个甚至像110个追随者,它不起作用。非常感谢任何帮助弄清楚如何使其适用于更大的数字!

这是我现在拥有的代码:

import tweepy
import time

key1 = "..."
key2 = "..."
key3 = "..."
key4 = "..."

accountvar = raw_input("Account name: ")

auth = tweepy.OAuthHandler(key1, key2)
auth.set_access_token(key3, key4)

api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name=accountvar).pages():
     ids.extend(page)
     time.sleep(60)

users = api.lookup_users(user_ids=ids)
for u in users:
     print u.screen_name

我不断收到的错误是:

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    users = api.lookup_users(user_ids=ids)
  File "/Library/Python/2.7/site-packages/tweepy/api.py", line 321, in lookup_users
    return self._lookup_users(post_data=post_data)
  File "/Library/Python/2.7/site-packages/tweepy/binder.py", line 239, in _call
    return method.execute()
  File "/Library/Python/2.7/site-packages/tweepy/binder.py", line 223, in execute
    raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{u'message': u'Too many terms specified in query.', u'code': 18}]

我已经查看了很多关于此类问题的其他问题,但我找不到适合我的解决方案,但如果有人有解决方案的链接,请将其发送给我!

最佳答案

我其实已经弄明白了,所以我把解决方案贴在这里,仅供引用。

import tweepy
import time

key1 = "..."
key2 = "..."
key3 = "..."
key4 = "..."

accountvar = raw_input("Account name: ")

auth = tweepy.OAuthHandler(key1, key2)
auth.set_access_token(key3, key4)

api = tweepy.API(auth)

users = tweepy.Cursor(api.followers, screen_name=accountvar).items()

while True:
    try:
        user = next(users)
    except tweepy.TweepError:
        time.sleep(60*15)
        user = next(users)
    except StopIteration:
        break
    print "@" + user.screen_name

这在每 300 个名字后停止 15 分钟,然后继续。这确保它不会遇到问题。对于大客户来说,这显然需要很长时间,但正如 Leb 所提到的:

The twitter API only allows 100 users to be searched for at a time...[so] what you'll need to do is iterate through each 100 users but staying within the rate limit.

如果您想要下一组,您基本上只需让程序保持运行即可。我不知道为什么我的一次给 300 而不是 100,但正如我之前提到的我的程序,它也更早给了我 100。

希望这对和我有同样问题的其他人有所帮助,并感谢 Leb 提醒我关注速率限制。

关于python - 如何获得大量关注者 Tweepy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000178/

相关文章:

python ,Tkinter : How to get the handle of all canvas objects using their IDs or tags?

Python CSV编辑数字大于特定数字的字段

python - Numpy 运行时警告 : divide by zero encountered in log10

python - twitter api 转发排除

api - 时间范围的 Twitter API 推文

python - 如何将大的 float 值转换为 int?

android - 使用 Twitter4j-4.0.2 消费和过滤推文

python - 流式传输 Twitter 推文时忽略转发

python - 将时间转换为纪元(Python)

python - Tweepy - 是否可以流式传输准确的短语?