使用Tweepy从Twitter API 2.0获取tweet_fields的问题



我有一个类似于这个问题的问题(从Twitter API 2.0获取user.fields的问题(但我用的是Tweepy。当使用tweet_fields发出请求时,响应只会给我默认值。在我使用user_fields的另一个函数中,它工作得很好。我遵循了这个指南,特别是17号(https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9)

我的功能如下:

def get_user_tweets():
client = get_client()
tweets = client.get_users_tweets(id=get_user_id(), max_results=5)
ids = []
for tweet in tweets.data:
ids.append(str(tweet.id))
tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
print(tweets_info)

这是我的回应(与elonmust的最后一条推文(也没有错误代码或任何其他

Response(data=[<Tweet id=1471419792770973699 text=@WholeMarsBlog I came to the US with no money &amp; graduated with over $100k in debt, despite scholarships &amp; working 2 jobs while at school>, <Tweet id=1471399837753135108 text=@TeslaOwnersEBay @PPathole @ScottAdamsSays @johniadarola @SenWarren It’s complicated, but hopefully out next quarter, along with Witcher. Lot of internal debate as to whether we should be putting effort towards generalized gaming emulation vs making individual games work well.>, <Tweet id=1471393851843792896 text=@PPathole @ScottAdamsSays @johniadarola @SenWarren Yeah!>, <Tweet id=1471338213549744130 text=link>, <Tweet id=1471325148435394566 text=@24_7TeslaNews @Tesla ❤️>], includes={}, errors=[], meta={})

我找到了这个链接:https://giters.com/tweepy/tweepy/issues/1670.据报道,

响应是一个命名的元组。在它的数据字段中,有一个Tweet对象。

Tweet对象的字符串表示将只包含其ID和文本。这是一个有意的设计选择,以减少在将所有数据打印为字符串表示时可能显示的过量信息,就像使用模型一样。地位ID和文本是唯一的默认/保证字段,因此字符串表示形式保持一致和唯一,同时仍然简洁。此设计在整个API v2模型中使用。

要访问Tweet对象的数据,可以使用属性或键(如字典(访问每个字段。如果要将所有数据作为字典,可以使用数据属性/键。

在这种情况下,要访问公共度量,您可以尝试这样做:

tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
for tweet in tweets_info.data:
print(tweet["id"])
print(tweet["public_metrics"])

最新更新