Tweety Twitter获取特定用户的所有推文回复

Zul*_*zmi 5 python api twitter tweepy python-2.7

我想收到这个特定用户的所有回复.所以这个特定的用户有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
Run Code Online (Sandbox Code Playgroud)

输出:

jji*_*ing 7

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

# 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
Run Code Online (Sandbox Code Playgroud)

变量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
Run Code Online (Sandbox Code Playgroud)

它不漂亮,但它完成了工作.祝好运!


Mal*_*aiq 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
Run Code Online (Sandbox Code Playgroud)