在 python 中使用 Tweepy 并跟随给定用户但不退出循环

Using Tweepy in python and its following a given user but not exiting the loop

获取给定的推文 ID 并将尝试关注该推文的用户。 当我查看 twitter 时,我可以看到它已成功关注用户,但我的 while 循环从未退出。(这一切都在控制台中)

def followUser(tweet):#Follows a user
    tweet = api.get_status(tweet.id)
    while tweet.user.follow_request_sent == False:
        api.create_friendship(tweet.user.id)
        followUser(tweet)
    print("Successful follow")

这是我 ctrl-c 时的错误信息

  File "<stdin>", line 2, in followUser
  File "/home/user1/.local/lib/python2.7/site-packages/tweepy/binder.py", line 250, in _call
    return method.execute()
  File "/home/user1/.local/lib/python2.7/site-packages/tweepy/binder.py", line 190, in execute
    proxies=self.api.proxy)
  File "/home/user1/.local/lib/python2.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/user1/.local/lib/python2.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/user1/.local/lib/python2.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/user1/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/user1/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/home/user1/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/home/user1/.local/lib/python2.7/site-packages/urllib3/connection.py", line 344, in connect
    ssl_context=context)
  File "/home/user1/.local/lib/python2.7/site-packages/urllib3/util/ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python2.7/ssl.py", line 369, in wrap_socket
    _context=self)
  File "/usr/lib/python2.7/ssl.py", line 617, in __init__
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 846, in do_handshake
    self._sslobj.do_handshake()

试试这个:

def followUser(tweet):#Follows a user
    tweet = api.get_status(tweet.id)
    while tweet.user.follow_request_sent == False:
        api.create_friendship(tweet.user.id)
        followUser(tweet)
        break
    print("Successful follow")

根据 @Haroldo_OK 建议使用 if 我添加了一个计数器:

def followUser(tweet): # Follows a user
    counter = 100
    while counter >= 0:
        tweet = api.get_status(tweet.id)
        # as [@harmon758][2] pointed out I forgot to subtract from the counter...
        counter -= 1
        if tweet.user.follow_request_sent == False:
            api.create_friendship(tweet.user.id)
            followUser(tweet)
            print("Successful follow")

希望对您有所帮助!

这应该有效。我拆分了刷新部分,因为我也在程序的其他部分调用它。

def followUser(tweet):#Follows a user given a tweet id input
    id = tweet.retweeted_status.user.id
    if api.get_user(id).following == False:
        try:
            api.create_friendship(id)
            return followUser(refresh(tweet))
        except tweepy.TweepError as e:
            pass

def refresh(tweet):
    return api.get_status(tweet.id)

这有多个问题。

据我所知,User objectfollow_request_sent 属性没有记录,但它似乎只有 True 当您向受保护的用户发送关注请求时帐户并且相关用户尚未接受它,因此即使您已经关注该用户,这也将是 False,并且在任何时候都不会是 True不受保护。这就是导致无限循环的原因。
相反,您应该像在回答中那样使用 following 属性。但是,请注意该属性已被弃用,因此使用 API.show_friendship 可能更为谨慎。
not 也应该用来代替 == False 如果你不期待任何其他虚假值,我不认为 follow_request_sent returns 和 following绝对不会return。 请注意,following 可以为空,这意味着 Twitter 的 API 可以 return "null",但是 Tweepy converts it to False.

你也在递归调用函数。
这与循环一起是完全没有必要的,也是非常糟糕的做法。

因为您已经有了 User 对象,您应该只使用它的 follow 辅助函数而不是使用 API.create_friendship。这还将 following 属性设置为 True,因此用户对象将保持更新。

如果 tweet 已经是 Status 对象,我不确定 tweet = api.get_status(tweet.id) 的用途是什么。您正在执行不必要的 API 请求以获取您已有的相同 Status 对象。您不需要像这样刷新 Status 对象。

在您的回答中,您还有 id = tweet.retweeted_status.user.id。请注意,如果相关状态不是转推,这将出错。您还在关注原始推文的作者而不是转推文并执行另一个不必要的 API 请求以获取您已有的相同用户对象。

请注意,如果您尝试关注的用户没有受保护的帐户,@Moshe Slavi's will infinitely loop because counter is not decremented and is always 100, and both @Moshe Slavi's answer and @user15051990's 将无限递归。

有关详细信息,请参阅 my response on GitHub