Tweepy Twitter获取特定用户的所有tweet回复

2024-04-26 03:11:17 发布

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

我正在尝试获取此特定用户的所有答复。所以这个特定的用户有151791801的用户id str的回复。我试着把所有的回复都打印出来,但不知道怎么打印。然而,我只能打印出其中的一个回复。有谁能帮我把所有的回复都打印出来吗?enter image description here

我的代码是:

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:


Tags: to代码text用户inapiidfor
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

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

# 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将包含它们最后一次转发给您的内容。从那里,你可以回到你原来的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

虽然不漂亮,但能完成任务。 祝你好运!

相关问题 更多 >