如何使用pythontwi找到follow required users with pythontwi

2024-04-29 00:42:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pythontwitter库跟踪列表中的某个用户。但是对于一些用户,我会出现“你已经请求使用用户名”的错误。这意味着我已经向该用户发送了以下请求,因此我不能再这样做了。那么我如何控制用户,我发出以下请求。或者有其他方法来控制它。在

for userID in UserIDs:
    api.CreateFriendship(userID)

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


Tags: 方法用户inapi编辑列表for错误
2条回答

这个问题被问到已经将近三年了,但作为参考,当你在谷歌上搜索这个问题时,它会成为热门话题。在

在这篇文章中,pythontwitter仍然是这样(即pythontwitter没有直接的方法来识别待处理的友谊或关注者请求)。在

也就是说,可以通过扩展API类来实现它。这里有一个例子:https://github.com/itemir/twitter_cli

相关片段:

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',[])

这里有两个选项:

  • 在循环之前调用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
    

希望有帮助。在

相关问题 更多 >