【问题标题】:How to get twitter followers using Twython?如何使用 Twython 获得 Twitter 关注者?
【发布时间】:2011-08-23 06:09:07
【问题描述】:

当指定用户的屏幕名称或 user.id 时,我想获取特定用户的 Twitter 关注者/关注者列表。任何人都可以请给它的代码sn-p吗?谢谢。

【问题讨论】:

  • 您需要显示您实际尝试过解决方案但失败的代码,以便我们正确解决您的问题。

标签: python twitter twitter-oauth twython


【解决方案1】:

我是 Twython 的作者。您可以使用两种不同的方法;一种只返回关注者 ID (get_followers_ids),一种返回关注者集的状态/等 (get_followers_list)。

一些示例代码如下所示:

from twython import Twython

twitter = Twython()
followers = twitter.get_followers_ids(screen_name = "ryanmcgrath")

for follower_id in followers:
    print "User with ID %d is following ryanmcgrath" % follower_id

如果您有 ID,则需要自己进行进一步查找,因此后一种方法 (get_followers_list) 可能是您想要的。请记住,Twython 函数只是从官方 Twitter API 文档中镜像 API 关键参数,因此您可以传递给参数的方法与您在文档中找到的方法相同。

祝你好运!

【讨论】:

  • 似乎这不再起作用了,从那时起 TwitterAPI 中可能发生了一些变化:打印“用户 %d 正在关注用户 X”% follower_id 类型错误:%d 格式:需要一个数字,而不是 unicode
  • 此评论旨在改进上面给出的答案。正如我的建议中所提到的,仅当您遵循 get_followers_ids() 返回的 json 结构时,打印关注者 ID 列表才有效: {u'previous_cursor': 0, u'previous_cursor_str': u'0', u'next_cursor' : xxxxxxxx, u'ids': [xxxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, ... for follower_id in follower['ids']: print "用户 %d 正在关注给定的用户 % follower_id 这就是所有人。谢谢。
【解决方案2】:

应该是:

followers = twitter.get_followers_list(screen_name = "whatever")

【讨论】:

    【解决方案3】:
    from twython import Twython
    
    twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    
    followers = twitter.get_followers_ids(id = 1234) # or just () - followers for your account
    
    print(twitter.get_followers_ids()['ids']) # ids list of followers
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2015-05-19
      • 2011-09-20
      相关资源
      最近更新 更多