【问题标题】:Tweepy Twitter get all tweet replies of particular userTweepy Twitter 获取特定用户的所有推文回复
【发布时间】:2015-10-08 11:05:26
【问题描述】:

我正在尝试获取该特定用户的所有回复。所以这个特定的用户的reply_to_user_id_str 为151791801。我试图打印出所有的回复,但我不确定如何。但是,我只能打印出其中的 1 个回复。谁能帮我把所有的回复打印出来?

我的代码是:

for page in tweepy.Cursor(api.user_timeline, id="253346744").pages(1):
    for item in page:
            if item.in_reply_to_user_id_str == "151791801":
                print item.text
                a = api.get_status(item.in_reply_to_status_id_str)
                print a.text

【问题讨论】:

    标签: python api python-2.7 twitter tweepy


    【解决方案1】:

    首先,找到您与服务提供商对话的转推线程:

    # Find the last tweet
    for page in tweepy.Cursor(api.user_timeline, id="253346744").pages(1):
        for item in page:
            if item.in_reply_to_user_id_str == "151791801":
                last_tweet = item
    

    变量last tweet 将包含他们最后一次转发给您的推文。从那里,您可以循环回到您的原始推文:

    # Loop until the original tweet
    while True:
        print(last_tweet.text)
        prev_tweet = api.get_status(last_tweet.in_reply_to_status_id_str)
        last_tweet = prev_tweet
        if not last_tweet.in_reply_to_status_id_str:
            break
    

    它并不漂亮,但它完成了工作。 祝你好运!

    【讨论】:

    • 谢谢!有用。只是一个问题,你知道如何避免推文限制吗?尝试异常但不起作用
    • 你的意思是 twitter API 速率限制吗?您可以在设置 api 时将 wait_on_rate_limit 参数设置为 true。例如:api = tweepy.API(auth_args, wait_on_rate_limit=True).
    【解决方案2】:
    user_name = "@nameofuser"
    
    replies = tweepy.Cursor(api.search, q='to:{} filter:replies'.format(user_name)) tweet_mode='extended').items()
    
    while True:
        try:
            reply = replies.next()
            if not hasattr(reply, 'in_reply_to_user_id_str'):
                continue
            if str(reply.in_reply_to_user_id_str) == "151791801":
               logging.info("reply of :{}".format(reply.full_text))
    
        except tweepy.RateLimitError as e:
            logging.error("Twitter api rate limit reached".format(e))
            time.sleep(60)
            continue
    
        except tweepy.TweepError as e:
            logging.error("Tweepy error occured:{}".format(e))
            break
    
        except StopIteration:
            break
    
        except Exception as e:
            logger.error("Failed while fetching replies {}".format(e))
            break
    

    【讨论】:

    • 我投了反对票,但答案实际上是正确的。解锁后会立即投票。
    • @RaunaqJain 没问题 :)
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 2017-02-11
    • 2016-05-18
    • 2016-06-16
    • 1970-01-01
    • 2011-12-27
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多