【问题标题】:How can I find follow requested users with python-twitter如何使用 python-twitter 找到关注请求的用户
【发布时间】:2013-06-28 22:11:37
【问题描述】:

我正在尝试使用 python-twitter 库在列表中关注某个用户。但是对于某些用户,我正在接受“您已经要求关注用户名”错误。这意味着我已向该用户发送了以下请求,因此我不能再这样做了。那么如何控制用户,我发送了以下请求。或者有没有其他方法可以控制它。

for userID in UserIDs:
    api.CreateFriendship(userID)

编辑:我正在总结:您可以随时关注一些用户。但有些人不让。首先您必须发送友谊请求,然后他/她可能会接受或不接受。我想学习的是,如何列出请求的用户。

【问题讨论】:

    标签: python twitter python-twitter


    【解决方案1】:

    你有两个选择:

    • 在循环前调用GetFriends

      users = [u.id for u in api.GetFriends()]
      for userID in UserIDs:
          if userID not in users:
              api.CreateFriendship(userID)
      
    • 使用try/except:

      for userID in UserIDs:
          try:
              api.CreateFriendship(userID)
          except TwitterError:
              continue
      

    希望对您有所帮助。

    【讨论】:

    • 我想学习的是,我如何才能看到请求友谊的用户。不是我接受的朋友。但是第二个选项可以工作。
    • 我认为没有办法使用 twitter API 获得待处理的友谊请求。所以,是的,你应该选择第二个选项。
    【解决方案2】:

    这个问题被问到已经快三年了,但是当你用谷歌搜索这个问题时,它会显示为最热门的问题。

    在这篇文章中,python-twitter 仍然是这种情况(即 python-twitter 没有直接的方法来识别待处理的友谊或追随者请求)。

    也就是说,可以扩展 API 类来实现它。此处提供了一个示例:https://github.com/itemir/twitter_cli

    相关sn-p:

    class ExtendedApi(twitter.Api):
        '''
        Current version of python-twitter does not support retrieving pending
        Friends and Followers. This extension adds support for those.
        '''
        def GetPendingFriendIDs(self):
            url = '%s/friendships/outgoing.json' % self.base_url
            resp = self._RequestUrl(url, 'GET')
            data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
    
        return data.get('ids',[]) 
    
        def GetPendingFollowerIDs(self):
            url = '%s/friendships/incoming.json' % self.base_url
            resp = self._RequestUrl(url, 'GET')
            data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
    
            return data.get('ids',[])
    

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2020-05-11
      • 2012-03-24
      • 2018-11-05
      • 2020-11-10
      • 2020-10-02
      相关资源
      最近更新 更多