Pra*_*dey 6 python twitter twython tweepy twitter4j
我正在使用 Pythontweepy库。
我使用以下代码成功提取了推文的“喜欢”和“转发”计数:
# 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
Run Code Online (Sandbox Code Playgroud)
但是,我找不到获取特定推文的回复计数的方法。
是否可以使用 tweepy 或任何其他库(如 twython 甚至 twitter4j)获取推文的回复计数?
下面的示例代码展示了如何实现一个解决方案来查找单个推文的所有回复。它利用 Twitter 搜索运算符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)
Run Code Online (Sandbox Code Playgroud)
这段代码的局限性是效率低下。它抓取了超出必要数量的推文。然而,这似乎是最好的方法。另外,如果您想获得一条非常旧的推文的回复,您可以max_id在 api.search 中实现该参数来限制您搜索回复的时间长度。
| 归档时间: |
|
| 查看次数: |
4201 次 |
| 最近记录: |