【问题标题】:Why do tweets that I know have likes have 0 return for their favorite count using the tweepy api?为什么我知道有喜欢的推文使用 tweepy api 返回他们最喜欢的计数为 0?
【发布时间】:2018-03-14 04:13:33
【问题描述】:

基本上,当我通过在 Twitter 上看到我知道已被“喜欢”的推文循环并打印他们最喜欢的计数属性时,计数始终为 0。最喜欢的计数与喜欢的次数不同/为什么最喜欢的计数始终为 0/我如何获得一条推文的点赞数?

现在我正在做以下事情:

print(the_tweet.favorite_count)

当我打印时:

print(dir(the_tweet)) 

我看到很多与推文有关的东西,包括 retweet_count 和 favorite_count,但没有一个看起来像“like_count”的东西。

【问题讨论】:

  • favorite_count: Integer,可为空。 "表示这条推文被 Twitter 用户点赞的大概次数。例如:"favorite_count":1138" developer.twitter.com/en/docs/tweets/data-dictionary/overview/…
  • 我以为它的意思是“喜欢计数”,但我只是不明白为什么我查找的每条推文实际上都是 0 - 太奇怪了。
  • 也许在这个线程中有一些有用/相关的信息:Retweet_count and favorite count always zero。 “新创建的转推具有非零 retweet_count 和 favorite_count 值作为“转推状态”节点的子节点,这取决于原始推文的状态是适当的/典型的......”等

标签: python twitter tweepy


【解决方案1】:

我刚刚使用它并为我工作。我遇到了同样的问题,发生这种情况是因为我使用了“tweepy”,它在 json api 中为 favorite_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 - get tweets with specific id

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,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 %}
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题。对我来说,问题是推文是转发,处理如下。包括一个新的 tweet.fields 值 referenced_tweets 和 public_metrics,它将返回任何引用的推文作为 ID。然后在新呼叫(相同路由)中使用此 ID 以及 public_metrics 来获取转发推文的真实指标。

      【讨论】:

        猜你喜欢
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多