python - 获取对特定推文的回复计数

标签 python twitter twitter4j tweepy twython

我正在使用 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)获取推文的回复计数?

最佳答案

下面的示例代码显示了如何实现一个解决方案来查找单个推文的所有回复。它利用推特搜索运算符 to:<account>并抓取所有回复该帐户的推文。通过使用 since_id=tweet_id ,推文从 api.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)
此代码的限制是效率低下。它抓取了比必要更多的推文。然而,这似乎是最好的方法。另外,如果你想得到一个很旧的推文的回复,你可以实现参数 max_id在 api.search 中限制您搜索回复的时间长度。

关于python - 获取对特定推文的回复计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46254912/

相关文章:

java - 显示 Twitter 流时阻止 twitter4j 打印输出

java - Twitter - 查询特定地理位置半径内的推文

python - 导入错误没有名为 OAuth 的模块 python twitter feed

安卓开发 |推文一个字符串

javascript - Tweet 按钮和信息窗口

android - twitter4j:根据 twitter 回调 url 获取访问 token

python - matplotlib中quiverkey的背景颜色

python Flask SQLAlchemy 将数据插入 mysql

python - cx_Freeze : "No module named ' codecs'"

python - 如何打印元素列表中当前索引的每三个元素