twitter api和oauth的python模块
twitter_oauth的Python项目详细描述
这是四个内容。
从0.1.0版更改为0.2.0版的内容
- 在0.2.0版中使用json数据
- 在0.2.0版中,您可以使用比0.1.0更多的方法
- 特别是,您可以在0.2.0版中使用搜索方法
必需
twitter_auth要求python 2.x优于2.5。
这些模块是必需的。
- OAuth2
如果使用Python2.5,还需要
- simplejson
安装
在Linux上。
安装simplejson
如果使用高于python 2.6的python2.x,请跳过install simplejson,转到install twitter_oauth
如果您使用python2.5,应该首先安装simplejson。
从http://pypi.python.org/pypi/simplejson/下载simplejson
然后,更改包含simplejson文件的目录,然后
$ tar vxzf simplejson-2.1.1.tar.gz $ cd simplejson-2.1.1 $ sudo python setup.py install
安装twitter_oauth
来自PYPI:
$ sudo easy_install twitter_oauth
教程
OAuth
首先,你应该有两把钥匙, “消费者密钥”、“消费者机密”。
如果你没有“消费者密钥”和“消费者秘密”, 你可以得到这些密钥来注册你的twitter应用程序。 你可以在下一个网址注册你的申请。
然后,您应该得到两个密钥“oauth_token”和“oauth_token_secret”
要获取这些密钥,请在此模块中使用getoauth类。
>>> import twitter_oauth >>> # write your key and secret >>> consumer_key = '***' >>> consumer_secret = '***' >>> get_oauth_obj = twitter_oauth.GetOauth(consumer_key, consumer_secret)
然后,使用get oauth方法获得“oauth_token”和“oauth_token_secret”。 此方法返回包含“使用者密钥”、“使用者机密”的字典, “oauth_token”和“oauth_token_secret”
>>> get_oauth_obj.get_oauth() Request Token: - oauth_token = *** - oauth_token_secret = *** Go to the following link in your browser http://twitter.com/oauth/authorize?oauth_token=*** What is the PIN? *** Access Token: - oauth_token = *** - oauth_token_secret = *** You may now access protected resources using the access token above
API等级
现在,您可以使用twitter_oauth.api类。 要使用本课程,您可以发布更新,或获取好友时间线等。
下一个例子是如何使用twitter_auth.api类
>>> # import twitter_oauth module >>> import twitter_oauth >>> # write yoru consumer_key, consumer_secret, >>> # oauth_token, oauth_token_secret >>> consumer_key = '***' >>> consumer_secret = '***' >>> oauth_token = '***' >>> oauth_token_secret = '***' >>> # Then, create Api instance >>> api = twitter_oauth.Api(consumer_key, consumer_secret, >>> oauth_token, oauth_token_secret)
使用“获取朋友”时间线方法。 你可以让朋友时间线使用这种方法。
>>> friends_timeline = api.get_friends_timeline() >>> print [stauts.text for status in friends_timeline] Use get_user_timeline method. You can get user timeline to use this method. >>> user_timeline = api.get_user_timeline() >>> print [stauts.text for status in user_timeline] Use get_replies method. You can get replies to use this method. >>> replies = api.get_replies() >>> print [stauts.text for status in replies]
使用后更新方法 你可以在twitter上发布消息。
注意:post_update方法应该采用unicode。 尤其是,你可以发一篇日文。
>>> api.post_update(tweet=u'Hello, Twitter')
使用get_list_status方法。
>>> # write username and list name >>> api.get_list_status(user='username', list_id='listname') >>> print [status.text for status in api.get_list_status(user="username", list_id="listname")]
使用搜索方法
如果要显示包含“keyword”的tweets,
>>> search_obj = api.search(q='keyword') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要显示包含“keyword”和“anotherkeyword”的tweets,
>>> search_obj = api.search(q='keyword anotherkeyword') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要显示包含“keyword”或“anotherkeyword”的tweets,
>>> search_obj = api.search(q='keyword OR anotherkeyword') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要显示“用户”的时间线,
>>> search_obj = api.search(q='from:user') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要向“用户”显示tweets,则
>>> search_obj = api.search(q='to:user') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要显示从“user”到“another”的tweets,则
>>> search_obj = api.search(q='from:user to:another') >>> print [tweet_info.text for tweet_info in search_obj.results]
如果要搜索标记,
>>> search_obj = api.search(q='#twitter') >>> print [tweet_info.text for tweet_info in search_obj.results]
要了解有关搜索方法的更多信息,请参阅下一个链接。
方法
您可以使用下一种方法
- 状态
- post_update()
- 显示状态()
- 销毁状态()
- 时间线
- 获取用户时间线()
- 获取好友时间线()
- 获取答复()
- 列表
- 获取列表状态()
- 友谊
- 建立友谊()
- 破坏友谊()
- 用户
- 搜索用户()
- 显示用户()
- 搜索
- 搜索()
示例代码
#! /usr/bin/env python # coding:utf-8 import twitter_oauth # write your oauth token and oauth token secret consumer_key = '***' consumer_secret = '***' # create GetOauth instance get_oauth_obj = twitter_oauth.GetOauth(consumer_key, consumer_secret) # get oauth_token and oauth token secret key_dict = get_oauth_obj.get_oauth() # create Api instance api = twitter_oauth.Api(consumer_key, consumer_secret, key_dict['oauth_token'], key_dict['oauth_token_secret']) ## timeline method # get friends timeline print [status.text for status in api.get_friends_timeline()] # get user timeline print [status.text for status in api.get_user_timeline()] # get replies print [status.text for status in api.get_replies()] ## status method # post update api.post_update(tweet=u'Hello, Twitter') # show status and destroy status status = api.get_user_timeline()[0] print api.show_status(id=status.id).text api.destroy_status(id=status.id) ## friendship method api.create_friendship(id='twitter') api.destroy_friendship(id='twitter') ## user method print api.show_user(id='twitter').screen_name print [user.screen_name for user in api.search_user(q='twitter')] ## search method print [tweet_info.text for tweet_info in api.search(q='#twitter').results]