特定Twitter用户不返回带有用户_timeline()的推文

2024-04-24 20:58:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从不同的用户那里获取最新的tweet,除了this user之外,我用完全相同的代码尝试过的每一条tweet都能正常工作。以下是我尝试的代码:

import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweet = api.user_timeline(screen_name='millions',
                          # 200 is the maximum allowed count
                          count=1,
                          include_rts=False,
                          tweet_mode='extended',
                          exclude_replies=True,
                          include_entities=True
                          )
print(tweet)

它只为我打印[],如果我将用户更改为其他用户,它将正常工作


1条回答
网友
1楼 · 发布于 2024-04-24 20:58:23

碰巧它带来了“count”的数量,然后过滤(在本例中是rt和reply)。从doc开始:

排除回复

Using exclude_replies with the count parameter will mean you will receive up-to count tweets — this is because the count parameter retrieves that many Tweets before filtering out retweets and replies.

包括\u rts

When set to false , the timeline will strip any native retweets (though they will still count toward both the maximal length of the timeline and the slice selected by the count parameter)

所以,如果你只带了一个,而它是rt(或回复),你将留下一个空列表

带1或200也是一样的,都算作一个api调用。因此,最好多带些东西,坚持第一条:

tweet = api.user_timeline(screen_name='millions',
                          count=200,
                          include_rts=False,
                          tweet_mode='extended',
                          exclude_replies=True,
                          include_entities=True
                          )
# keep only the first tweet
tweet = tweet[:1]

相关问题 更多 >