我正在尝试使用Tweepy
从Twitter API v2下载特定用户的推文。出于某种原因,当我将organic_metrics
添加到tweet_fields
时,我得到一个空响应。如果我删除organic_metrics
,我会收到预期的响应推文。
这是MWE。
import configparser
from pprint import PrettyPrinter
import tweepy
import logging
# Read Twitter authentication information from settings.ini
config = configparser.RawConfigParser()
config.read("settings.ini")
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
pp = PrettyPrinter()
# Setup access to API
def connect_to_twitter_OAuth():
client = tweepy.Client(
consumer_key=config["AUTHENTICATION"]["CONSUMER_KEY"],
consumer_secret=config["AUTHENTICATION"]["CONSUMER_SECRET"],
access_token=config["AUTHENTICATION"]["ACCESS_TOKEN"],
access_token_secret=config["AUTHENTICATION"]["ACCESS_SECRET"],
wait_on_rate_limit=True,
)
_logger.debug("Authenticated with Twitter with user context.")
return client
# Create API object
api = connect_to_twitter_OAuth()
tweet_fields = [
"id",
"text",
"conversation_id",
"created_at",
"in_reply_to_user_id",
"organic_metrics",
]
paginator = tweepy.Paginator(
api.get_users_tweets,
user_id,
max_results=5,
tweet_fields=tweet_fields,
user_auth=True,
)
_logger.debug("Retrieving tweets from user.")
for tweet in paginator.flatten(limit=250):
pp.pprint(tweet)
_logger.debug("Tweet retrieved.")
有什么想法吗?
"非公开、有机和推广的指标仅适用于过去30天内创建的推文">
https://developer.twitter.com/en/docs/twitter-api/metrics
这就是为什么你不能在回复中分页。希望这能回答你的一些困惑!