获取特定推文的回复数

6

我正在使用Python的tweepy库。

我成功地使用以下代码提取了推文的“赞”和“转发”计数:

# Get count of handles who are following you
def get_followers_count(handle):
    user = api.get_user(handle)
    return user.followers_count

# Get count of handles that you are following
def get_friends_count(handle):
    user = api.get_user(handle)
    return user.friends_count

# Get count of tweets for a handle
def get_status_count(handle):
    user = api.get_user(handle)
    return user.statuses_count

# Get count of tweets liked by user
def get_favourite_count(handle):
    user = api.get_user(handle)
    return user.favourits_count

然而,我找不到获取特定推文回复计数的方法。

使用 tweepy 或其他库(如 twython 或 twitter4j),是否有可能获取推文的回复计数?


可能是重复问题 https://dev59.com/R3E85IYBdhLWcg3wnU0d - Zero
我请求您删除重复的标签,因为这个答案非常老旧。 - Prakash P
重新开放,但没有太多变化,我会让其他人决定是保持开放还是关闭。 - Zero
这个回答解决了你的问题吗?[回复特定推文,Twitter API](https://dev59.com/R3E85IYBdhLWcg3wnU0d) - Joe Mayo
@Zero,我认为你是对的——我也投了关闭票。 - Joe Mayo
1个回答

2
下面的示例代码展示了如何实现查找单条推文的所有回复的解决方案。它利用Twitter搜索运算符to:<account>并获取所有回复该帐户的推文。通过使用since_id=tweet_idapi.search返回的推文被限制为在发帖时间之后创建的推文。获取这些推文之后,使用in_reply_to_status_id属性来检查捕获到的推文是否是所关注的推文的回复。
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
api = tweepy.API(auth)

user = 'MollyNagle3'
tweet_id = 1368278040300650497
t = api.search(q=f'to:{user}', since_id=tweet_id,)

replies = 0
for i in range(len(t)):

    if t[i].in_reply_to_status_id == tweet_id:
        replies += 1
print(replies)

这段代码的限制在于效率低下,它获取了比必要数量更多的推文。不过,这似乎是目前最好的方法。此外,如果你想获取一条非常老的推文的回复,可以在api.search中实现参数max_id来限制搜索回复的时间长度。

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