tweepy 没有给我最后一条推文
tweepy is not giving me the last tweet
我正在尝试使用 python 中的 tweepy 从 Twitter 帐户获取最后一条推文。
我知道有一些类似的答案,例如这个
然而,我遇到的问题是我没有从个人时间轴中获取最后一条推文,而是倒数第二条推文。
我在 while 循环中使用这段代码:
tweetL = api.user_timeline(screen_name='elonmusk', tweet_mode="extended", exclude_replies, count=1)
print(tweetL[0].full_text)
如果我 运行 这个(在撰写本文时),我从 elon 那里得到这条推文:
What a beautiful day in LA
但是看他的时间线,他的最后一条推文是这样的:
Warm, sunny day & snowy mountains
那么为什么我没有收到最后一条推文?
奇怪的是 运行 昨晚使用这个脚本确实打印出了他的最后一条推文。
运行现在我收到了同样的推文,它是昨天打印出来的最后一条推文
如果我 运行 上面的代码是这样的(没有 'exclude_replies')
tweetL = api.user_timeline(screen_name='elonmusk', tweet_mode="extended")
print(tweetL[0].full_text)
我得到他的最后一条推文
@ErcXspace @smvllstvrs T/W will be ~1.5, so it will accelerate unusually fast. High T/W is important for reusable vehicles to make more efficient use of propellant, the primary cost. For expendable rockets, throwing away stages is the primary cost, so optimization is low T/W.
这是他最后的回复,所以这个有效。
我只是无法从他的时间线上获取最后一条实际推文
如评论中Iain Shelvington所述,exclude_replies
也会忽略对自己的回复。
我认为没有直接的方法可以在他们的时间轴中获取用户的最后一条推文。您可以创建一个函数,从检索到的推文中获取第一个:
a) 不是回复,即 in_reply_to_screen_name = None
.
b) 或回复自己,即 in_reply_to_screen_name = screen_name
.
这可能类似于:
def get_last_timeline_tweet(screen_name: str, tweets: list):
for tw in tweets:
if (tw.in_reply_to_screen_name is None or
tw.in_reply_to_screen_name == screen_name):
return tw
return None
然后,运行:
last_timeline_tweet = get_last_timeline_tweet('elonmusk', tweetL).full_text
print(last_timeline_tweet)
你得到:
Warm, sunny day & snowy mountains [url to the photo]
这也可以在一行中完成:
screen_name = 'elonmusk'
last_tweet = next((tw for tw in tweetL if tw.in_reply_to_screen_name is None
or tw.in_reply_to_screen_name == screen_name), None)
print(last_tweet.full_text)
注意:在得到它的full_text
.
之前应该检查last_tweet
不是None
我正在尝试使用 python 中的 tweepy 从 Twitter 帐户获取最后一条推文。
我知道有一些类似的答案,例如这个
然而,我遇到的问题是我没有从个人时间轴中获取最后一条推文,而是倒数第二条推文。
我在 while 循环中使用这段代码:
tweetL = api.user_timeline(screen_name='elonmusk', tweet_mode="extended", exclude_replies, count=1)
print(tweetL[0].full_text)
如果我 运行 这个(在撰写本文时),我从 elon 那里得到这条推文:
What a beautiful day in LA
但是看他的时间线,他的最后一条推文是这样的:
Warm, sunny day & snowy mountains
那么为什么我没有收到最后一条推文? 奇怪的是 运行 昨晚使用这个脚本确实打印出了他的最后一条推文。 运行现在我收到了同样的推文,它是昨天打印出来的最后一条推文
如果我 运行 上面的代码是这样的(没有 'exclude_replies')
tweetL = api.user_timeline(screen_name='elonmusk', tweet_mode="extended")
print(tweetL[0].full_text)
我得到他的最后一条推文
@ErcXspace @smvllstvrs T/W will be ~1.5, so it will accelerate unusually fast. High T/W is important for reusable vehicles to make more efficient use of propellant, the primary cost. For expendable rockets, throwing away stages is the primary cost, so optimization is low T/W.
这是他最后的回复,所以这个有效。
我只是无法从他的时间线上获取最后一条实际推文
如评论中Iain Shelvington所述,exclude_replies
也会忽略对自己的回复。
我认为没有直接的方法可以在他们的时间轴中获取用户的最后一条推文。您可以创建一个函数,从检索到的推文中获取第一个:
a) 不是回复,即 in_reply_to_screen_name = None
.
b) 或回复自己,即 in_reply_to_screen_name = screen_name
.
这可能类似于:
def get_last_timeline_tweet(screen_name: str, tweets: list):
for tw in tweets:
if (tw.in_reply_to_screen_name is None or
tw.in_reply_to_screen_name == screen_name):
return tw
return None
然后,运行:
last_timeline_tweet = get_last_timeline_tweet('elonmusk', tweetL).full_text
print(last_timeline_tweet)
你得到:
Warm, sunny day & snowy mountains [url to the photo]
这也可以在一行中完成:
screen_name = 'elonmusk'
last_tweet = next((tw for tw in tweetL if tw.in_reply_to_screen_name is None
or tw.in_reply_to_screen_name == screen_name), None)
print(last_tweet.full_text)
注意:在得到它的full_text
.
last_tweet
不是None