gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:28:29 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com