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应用程序。 你可以在下一个网址注册你的申请。

http://twitter.com/apps

然后,您应该得到两个密钥“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]

要了解有关搜索方法的更多信息,请参阅下一个链接。

http://dev.twitter.com/doc/get/search

方法

您可以使用下一种方法

  • 状态
    • 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]

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
另一个布局上的java Access文本视图   安卓在Java中,我什么时候应该用*导入整个包,而不是从包中导入单个对象?   JavaSpringMVC:请解释@RequestParam和@ModelAttribute之间的区别   java Flyway Ant构建未迁移   java“没有可供下载的文件”   如何解决java静态名称冲突?   我是否需要框架来补充JavaEE6、JSF2 WebApp?哪一个?   java如何传递HttpServletRequest参数?   只有java的视频不会播放声音。为什么?   java在Maven3中做这样的属性重写工作吗?   java计算Android中两个标记之间的距离   Javascript页面加载中的java复选框持久性问题   java序列化lambda函数的映射   java使用jersey、maven和eclipse配置swagger   java我可以在oncreate方法之外使用setContentView吗?   java在使用JAXRS响应类返回实体时遇到异常   java规范了加密和解密文本的文本编写方法   java如何更改ChoiceBox的默认大小?   java在Android上暂时禁用PIN/密码锁