Twitter API - 获取具有特定 id 的推文 [英] Twitter API - get tweets with specific id

查看:70
本文介绍了Twitter API - 获取具有特定 id 的推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个推文 ID 列表,我想下载它们的文本内容.是否有任何简单的解决方案可以做到这一点,最好是通过 Python 脚本?我查看了其他库,如 Tweepy,但事情似乎并不那么简单,手动下载它们是不可能的,因为我的列表很长.

解决方案

您可以通过 statuses/show/:id API 路由.大多数 Python Twitter 库都遵循完全相同的模式,或者为方法提供友好"名称.

例如,Twython 提供了多种 show_* 方法,包括 Twython.show_status() 让您加载特定推文:

CONSUMER_KEY = "<消费者密钥>"CONSUMER_SECRET = "<消费者秘密>"OAUTH_TOKEN = "<应用程序密钥>"OAUTH_TOKEN_SECRET = "<应用机密"推特 = Twython(CONSUMER_KEY, CONSUMER_SECRET,OAUTH_TOKEN、OAUTH_TOKEN_SECRET)推文 = twitter.show_status(id=id_of_tweet)打印(推文['文本'])

并且返回的字典遵循 API 给出的 Tweet 对象定义.>

tweepy 使用 tweepy.get_status():

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)api = tweepy.API(auth)推特 = api.get_status(id_of_tweet)打印(推文.文本)

它返回一个稍微丰富的对象,但它的属性再次反映了发布的 API.

I have a list of tweet ids for which I would like to download their text content. Is there any easy solution to do this, preferably through a Python script? I had a look at other libraries like Tweepy and things don't appear to work so simple, and downloading them manually is out of the question since my list is very long.

解决方案

You can access specific tweets by their id with the statuses/show/:id API route. Most Python Twitter libraries follow the exact same patterns, or offer 'friendly' names for the methods.

For example, Twython offers several show_* methods, including Twython.show_status() that lets you load specific tweets:

CONSUMER_KEY = "<consumer key>"
CONSUMER_SECRET = "<consumer secret>"
OAUTH_TOKEN = "<application key>"
OAUTH_TOKEN_SECRET = "<application secret"
twitter = Twython(
    CONSUMER_KEY, CONSUMER_SECRET,
    OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

tweet = twitter.show_status(id=id_of_tweet)
print(tweet['text'])

and the returned dictionary follows the Tweet object definition given by the API.

The tweepy library uses tweepy.get_status():

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
api = tweepy.API(auth)

tweet = api.get_status(id_of_tweet)
print(tweet.text)

where it returns a slightly richer object, but the attributes on it again reflect the published API.

这篇关于Twitter API - 获取具有特定 id 的推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆