使用Tweepy获取特定用户的所有推文回复

8
我想获取这个特定用户的所有回复。所以这个特定用户的reply_to_user_id_str是151791801。我尝试打印出所有回复,但我不确定该怎么做。然而,我只能打印出其中一个回复。有谁可以帮我打印出所有回复吗?
我的代码如下:
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

Output:

2个回答

11

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

# 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

它不够美观,但能完成工作。祝好运!


谢谢!它有效。只有一个问题,你知道如何避免推文限制吗?尝试了异常但不起作用。 - Zul Hazmi
你是指 Twitter API 的速率限制吗?在设置 API 时,您可以将 wait_on_rate_limit 参数设置为 true。例如:api = tweepy.API(auth_args, wait_on_rate_limit=True) - tonyslowdown

5
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

我给它点了踩,但实际上答案是正确的。一旦解锁,我会点赞的。 - Raunaq Jain
@RaunaqJain 没问题 :) - Malik Faiq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接