如何获取使用 tweepy 库的 update_status 方法生成的推文的推文 ID

How to get the tweet id for the tweet generated using tweepy library's update_status method

通过 tweepy post 一条新推文,我使用了以下代码

#Twitter credentials - Augrav
access_token = config.get('twitter_credentials', 'access_token')
access_token_secret = config.get('twitter_credentials', 'access_token_secret')
consumer_key = config.get('twitter_credentials', 'consumer_key')
consumer_secret = config.get('twitter_credentials', 'consumer_secret')
account_id = config.get('twitter_credentials', 'account_id')

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = API(auth)
msg = "Howdy howdy"
api.update_status(status=msg)

如何获取这个新生成的推文 ID?

tweepy 官方文档中没有关于此的信息。 参考:http://docs.tweepy.org/en/v3.2.0/api.html

这比您可能愿意相信的要微不足道。

documentation 可以看出 update_status 方法 returns 一个 status 对象。与 API 中的大多数方法一样。这个 status 对象基本上包含了关于那条推文的所有信息。

# after establishing a connection
In [15]: msg = "Tweeting from tweepy"

# retain the object returned
In [16]: tweet = api.update_status(status=msg)

# the id (and practically any attribute, print a tweet to view them!)
# can be easily accessed via
In [17]: tweet.id_str
Out[17]: u'646306396464656384'