【问题标题】:Get count of reply to a particular tweets获取对特定推文的回复计数
【发布时间】:2018-02-25 13:14:19
【问题描述】:

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

【问题讨论】:

标签: python twitter twitter4j tweepy twython


【解决方案1】:

下面的示例代码展示了如何实现一个解决方案来查找单个推文的所有回复。它利用推特搜索运算符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)

此代码的局限性在于效率低下。它抓取了比必要更多的推文。但是,这似乎是最好的方法。另外,如果你想得到一条很老的推文的回复,你可以在 api.search 中实现参数max_id 来限制你搜索回复的时间长度。

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2015-07-07
    • 2016-10-20
    • 2012-10-02
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多