为什么我知道喜欢的推文有0个返回的返回,可以使用Tweepy API来获得他们最喜欢的计数



基本上,当我通过在Twitter上看到我所知道的"喜欢"的推文并打印他们喜欢的计数属性时,计数始终为0。因为喜欢的数量/为什么最喜欢的计数总是0/我如何获得Tweet的数量?

现在我正在做以下操作:

print(the_tweet.favorite_count)

当我打印时:

print(dir(the_tweet)) 

我看到了很多有关推文的事情,包括retweet_count和fairmy_count,但没有看起来像" like_count"的东西。

我只是使用了它并为我工作。我遇到了同样的问题,这是因为我使用的是" tweepy",并且它在JSON API中返回了Faivy_count的两个值,一个值正确,另一个具有" 0"。当您使用" Twython"时,它仅返回一个值,第一个值。

pip install twython
from twython import Twython
id_of_tweet = <tweet_number_id>
CONSUMER_KEY = "<consumer key>"
CONSUMER_SECRET = "<consumer secret>"
OAUTH_TOKEN = "<application key>"
OAUTH_TOKEN_SECRET = "<application secret>"
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, 
OAUTH_TOKEN_SECRET)
tweet = twitter.show_status(id=id_of_tweet)
print(tweet)
print(tweet['retweet_count'])
print(tweet['favorite_count'])

此解决方案的一部分是在此松弛中:Twitter API-获取具有特定ID

的推文

i也有相同的问题, favorite_count显示了您自己的推文,而 the_tweet对象是您的user_timeline对象,它是推文和转发的混合物。因此,您需要检查一下retweeted_status是否存在,如果转发推文的favorite_count在此列表之下。请在下面查看我的示例:

def user_tweets(count):
    '''
    This function uses tweepy to read the user's twitter feed. 
    '''
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    # user = api.get_user(twitter_user_name)
    user_tweets = api.user_timeline(twitter_user_name, count=count)
    return user_tweets
user_tweets(4)# returns your latest 4 tweets and retweets
#to list the number of likes for each tweet and retweet:
for tweet in user_tweets: 
    try: 
        print(tweet.retweeted_status.favorite_count) 
    except: 
        print(tweet.favorite_count)

另外,如果您使用的是Django模板标签,则可以使用以下内容:

            {% if tweet.retweeted_status %}
            <small>{{ tweet.retweeted_status.favorite_count }}</small>
            {% else %}
            <small>{{ tweet.favorite_count }}</small>
            {% endif %}

我也有类似的问题。对我来说,问题是该推文是转发的,如下所示。在public_metrics中包括一个新的Tweet.fields值引用_tweets,它将返回任何引用的Tweet作为ID。然后在新调用(同一路由)中使用此ID与public_metrics一起获取转发推文的真实指标。

它对我有用


try:
    likecount = tweet._json['retweeted_status']['favorite_count']
except:
    likecount = 0

最新更新