Tweepy Get Tweets in reply to a particular tweet

≡放荡痞女 提交于 2021-01-27 17:44:22

问题


So I've been doing a lot of work with Tweepy and Twitter data mining, and one of the things I want to do is to be able to get all Tweets that are replies to a particular Tweet. I've seen the Search api, but I'm not sure how to use it nor how to search specifically for Tweets in reply to a specific Tweet. Anyone have any ideas? Thanks all.


回答1:


user_name = "@nameofuser"

replies = tweepy.Cursor(api.search, q='to:{}'.format(user_name),
                                since_id=tweet_id, tweet_mode='extended').items()

while True:
    try:

        reply = replies.next()
        if not hasattr(reply, 'in_reply_to_status_id_str'):
            continue
        if str(reply.in_reply_to_status_id) == tweet_id:
           logging.info("reply of tweet:{}".format(reply.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



回答2:


I've created a workaround that kind of works. The best way to do it is to search for mentions of a user, then filter those mentions by in_reply_to_id .



来源:https://stackoverflow.com/questions/37897064/tweepy-get-tweets-in-reply-to-a-particular-tweet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!