如何使用 V2 API 在 Tweepy 中获取用户 screen_name?
How to get user screen_name in Tweepy with V2 API?
我专门将 V2 API 与 tweepy 一起使用,并且正在努力获取用户 screen_name 的内容。我拥有的最好的是:
tweets = client.search_recent_tweets(query=query,expansions='author_id',max_results=10)
这需要往返才能获得 user_name。一定有一种方法可以在单个查询中完成吗?
此外,screen_name 是 V2 中的用户名,但我仍然无法理解它,因为它不是字段或扩展的可接受参数。
引用https://developer.twitter.com/en/docs/twitter-api/users/lookup/migrate/standard-to-twitter-api-v2
好吧,经过更多的搜索......它并不明显:
tweets = client.search_recent_tweets(query=query,tweet_fields=['context_annotations', 'created_at'], expansions='entities.mentions.username',max_results=10)
但是上面提到的user.fields参数还是不行
然而用户名在 user.fields
枚举中列为可用字段。
您可以将其添加到您的最后一个请求中:
tweets = client.search_recent_tweets(query=query,tweet_fields=['context_annotations', 'created_at'], expansions='entities.mentions.username', max_results=10, user_fields=['username']
但是如果您想要推文作者的用户名,您应该使用 author_id
扩展。
另请注意,用户名将在 includes
字典(在根目录)中,而不是在 data
字典中。
我专门将 V2 API 与 tweepy 一起使用,并且正在努力获取用户 screen_name 的内容。我拥有的最好的是:
tweets = client.search_recent_tweets(query=query,expansions='author_id',max_results=10)
这需要往返才能获得 user_name。一定有一种方法可以在单个查询中完成吗?
此外,screen_name 是 V2 中的用户名,但我仍然无法理解它,因为它不是字段或扩展的可接受参数。
引用https://developer.twitter.com/en/docs/twitter-api/users/lookup/migrate/standard-to-twitter-api-v2
好吧,经过更多的搜索......它并不明显:
tweets = client.search_recent_tweets(query=query,tweet_fields=['context_annotations', 'created_at'], expansions='entities.mentions.username',max_results=10)
但是上面提到的user.fields参数还是不行
然而用户名在 user.fields
枚举中列为可用字段。
您可以将其添加到您的最后一个请求中:
tweets = client.search_recent_tweets(query=query,tweet_fields=['context_annotations', 'created_at'], expansions='entities.mentions.username', max_results=10, user_fields=['username']
但是如果您想要推文作者的用户名,您应该使用 author_id
扩展。
另请注意,用户名将在 includes
字典(在根目录)中,而不是在 data
字典中。