开始
简介
如果你是第一次接触Tweepy,就请从这里开始。这个教程的目标是提供你学习Tweepy所需的信息,让你学习完本教程后能熟练使用Tweepy。我们在这主要谈论重要的基础内容,而不会涉及太多细节,
你好 Tweepy
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
这个例子可以下载你Twitter主页上的推文,并且把相应的文本内容打印到控制台。Twitter要求所有请求(requests)都通过OAuth协议进行授权(身份认证)。Authentication Tutorial(身份认证教程)(链接)中有授权的详细介绍。
API
API类为Twitter的所以REST API方法提供接口(The API class provides access to the entire twitter RESTful API methods.)每种方法接受不同的参数,但是都返回response。更多请参见API Reference(链接)
模型
当我们使用一种API方法时,我们大多数情况下会得到一个Tweepy model 类实例,其中包含了从Twitter返回的可以让我们应用到app中的数据。比如下面这行代码就返回了一个User model:
# Get the User object for twitter...
user = api.get_user('twitter')
Model中包含了数据和一些有用的方法:
print user.screen_name
print user.followers_count
for friend in user.friends():
print friend.screen_name
更多内容请参见 ModelsReference(链接)
原文链接:
http://tweepy.readthedocs.io/en/v3.5.0/getting_started.html