HOME > > python twitter <

python twitter <

Python (tweepy )篏帥ctwitter 腮帥茯サ違篆膊<

鐚2013綛7 菴処鐚菴 twitter API 1.0 篏帥1.1 篏帥違ctweepy 違若吾с 2.7 荅API 1.1 絲上<純紊眼荀<≪緇 twitte API 緇 tweepy т戎c<≪障鐚

鐚篋羣ゃhttp://jmillerinc.com/2010/05/31/twitter-from-the-command-line-in-python-using-oauth/ c

鐚tweepy ゃhttp://joshthecoder.github.com/tweepy/docs/ 荀

篋羣

茯荐惹劫 OAuth ctwitter ≪ ID 榊畿若吟劫≪絲障≪潟吾荐怨с荐怨ゃс羔сc違鐚OAuth 篁腟帥ゃhttp://tools.ietf.org/html/rfc5849 с鐚

(1) twitter ≪脂蚊CONSUMER KEYCONSUMER SECRET

茲違鴻吾翫с CONSUMER KYE CONSUMER SECRET 篏帥障違с脂蚊筝綺違篏罐違違違荀鴻若潟激сゃ茹h荀ゃ

糸с≪若吟障 twitter 茯荐主脂≪絨若吾с若吟≪絲障≪潟吾≪祉壕荐若twitter 鴻若醐callback URL鐚若吟蕋違菴篁腟帥絎憗≪肴 callback URL twitter 鏆荀≪脂我茵≪脂峨 https://dev.twitter.com/apps ≪祉鴻荐絎違筝鴻若吾ц帥鴻篏帥翫с茯荐弱若吟 twitter 劫с若吾蕋違綽荀сcallback URL twitter 脂蚊翫twitter 茯荐弱若吟≪若吾ゃ若吟絲障腱絲垩茵腓冴若吟垩с潟≪茯荐若絎篋bot 篏緇ч

(2) twitter ≪潟≪≪祉鴻荐怨

(1)х脂蚊≪絲障 twitter ≪潟吾≪祉鴻荐怨 twitter ACCESS KEYACCESS SECRET 肴с≪篁ヤ隙篏罐絲乗援c帥罕絖鐚callback URL 篏帥箴鐚

肢申荐鐚2014綛114ャtwitter API 吾≪祉鴻 TLS/SSL ・膓сcс篁ヤ絨紊眼綽荀cautu= у障膊с,secure=True

$ python
>>> import tweepy
>>> CONSUMER_KEY = '********************'
>>> CONSUMER_SECRET = '*******************'
>>> #鐚筝篋茵(1)сc若激若ャ鐚
>>> auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET,secure=True)
>>> auth_url = auth.get_authorization_url()
>>> print 'Please authorize: ' + auth_url
Please authorize: http://twitter.com/oauth/authorize?oauth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>> #鐚т茵腓冴 URL 吟綣泣篁篏帥援垩 web 若吾蚊鐚
>>> verifier = raw_input('PIN: ').strip()
PIN: xxxxxxxx 
>>> #鐚筝 PIN: с腮c援垩潟鐚
>>> auth.get_access_token(verifier)
<tweepy.oauth.OAuthToken object at 0x8f9166c>
>>> print "ACCESS_KEY = '%s'" % auth.access_token.key
ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>> #鐚筝茵腓冴≪祉鴻若緇障т戎с<≪鐚
>>> print "ACCESS_SECRET = '%s'" % auth.access_token.secret
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>> #鐚筝茵腓冴≪祉鴻激若緇障т戎с<≪鐚

(2)荐怨 twitter ≪潟羔с翫(2)膓筝綺茵c≪祉鴻若≪祉鴻激若緇

篁ヤфャ CONSUMER KEYCONSUMER SECRET ACCESS KEYACCESS SECRET篏帥c篏罐違 4 ゃ絖膣紊宴https://dev.twitter.com/apps х∈茯с

腮水鴻

#!/usr/bin/env python
import sys
import tweepy
CONSUMER_KEY = 'xxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxx'
mypost = sys.stdin.readline().strip()
if 0==len(mypost):
    exit()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, secure=True)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(mypost)

激с莎桁筝綣違絖腮帥с腱 GNOME 医с

alias twip='zenity --text-info --filename=/dev/null --editable | python /pass/to/the/script.py'

激сゃ≪鴻吾<c GUI 腮帥篏帥c

twikabi

ゃс潟潟ゃ潟сゃ障篏帥若ゃ≪潟吾帥鐚API 1.1 絲上鐚

twikabi

絲乗援医ф腮帥帥ゃゃ潟茯

障篁ヤ綽灸c api 吾с緇

$ python
>>> import tweepy
>>> import type
>>> CONSUMER_KEY = ''
>>> CONSUMER_SECRET = ''
>>> ACCESS_KEY = ''
>>> ACCESS_SECRET = ''
>>> #鐚筝4茵с肴c潟鐚
>>> auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET,secure=True)
>>> auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
>>> api = tweepy.API(auth, api_root='/1.1')

腮帥鴻帥

>>> api.update_status('test')
<tweepy.models.Status object at 0x90ec66c>

帥ゃゃ潟緇

>>> api.home_timeline()

逸鐚若祉帥ゃゃ潟腮帥鴻緇鐚茯с鐚

鴻荀膣

<class 'tweepy.models.Status'>

ゃ潟鴻帥潟鴻с腮随=鴻若帥刻蚊ゃ蚊ゃ絲上

篁ヤ紊違ゃ

api class 'tweepy.api.API'
contributors type 'NoneType'
truncated type 'bool'
text type 'unicode'腮水絎
created_at type 'datetime.datetime'腮炊
author class 'tweepy.models.User'腮粋私
in_reply_to_status_id type 'NoneType'ゅ乗院c腮帥鐚鐚
coordinates type 'NoneType'
source_url type 'str'
source type 'str'
favorited type 'bool'
in_reply_to_screen_name type 'NoneType'ょ御鐚
in_reply_to_user_id type 'NoneType'ょ御鐚鐚わ倶逸
place type 'NoneType'
geo type 'NoneType'
id type 'long'腮帥鐚鐚
user class 'tweepy.models.User'

鐚ャ<潟荀ゃ__dict__ 茯帥鴻 type 罨障乗帥鴻吾сゃc none c篆∞с鐚

鴻絎鴻text <type 'unicode'> 逸鐚鐚腮炊

>>> api.home_timeline()[0].text

у

筝ф腮粋author 紊違腓冴 <class 'tweepy.models.User'> ゃ潟鴻帥潟鴻ссゅ違茯帥鴻帥

profile_use_background_image type 'bool'
id type 'int'
api class 'tweepy.api.API'
verified type 'bool'
profile_sidebar_fill_color type 'str'
profile_text_color type 'str'
followers_count type 'int'
profile_sidebar_border_color type 'str'
location type 'str'
profile_background_color type 'str'
utc_offset type 'int'
statuses_count type 'int'
description type 'unicode'
friends_count type 'int'
profile_link_color type 'str'
profile_image_url type 'str'
notifications type 'bool'
geo_enabled type 'bool'
profile_background_image_url type 'str'
screen_name type 'str'
lang type 'str'
profile_background_tile type 'bool'
favourites_count type 'int'
name type 'unicode'
url type 'str'
created_at type 'datetime.datetime'
contributors_enabled type 'bool'
time_zone type 'str'
protected type 'bool'
following type 'bool'

鐚ャ<潟荀ゃ__dict__ 茯帥鴻 type 罨障乗帥鴻吾сゃc none c篆∞с鐚

c逸鐚鐚腮帥ゃapi.home_timeline()[0].author.name уapi.home_timeline()[0].author.screen_name с鴻若潟若鐚@緇ゃわャс

障帥ゃゃ恰違20筝茯c

с20c緇

障 home_timeline() <純蕋<若帥荀鐚http://joshthecoder.github.com/tweepy/docs/api.html#timeline-methods鐚

API.home_timeline([since_id][, max_id][, count][, page])其
* since_id  医違 ID ゃ若緇
* max_id  医ゃ ID ゃゃ若緇
* count  緇ゃ若
* page  緇若吾絎

ゃ障

>>> api.home_timeline(count=30)

30ゃ若緇

http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-home_timeline May not be greater than 200 吾

若吾潟違篏帥違cャс

>>> api.home_timeline(count=200, page=2)

違200篁吟ゆ違茵腓冴翫2若悟ゃ障違違201篁吟400篁句障с緇 鴻狗

http://apiwiki.twitter.com/Things-Every-Developer-Should-Know#6Therearepaginationlimits 荀若吾潟у 3200 篁吟障с

筝綺緇с篁倶違сゃ腮帥若吾潟篏帥c≪祉鴻с鐚 API 罧潟 http://apiwiki.twitter.com/Rate-limiting ョ狗с鐚

tweepy 若純罘

tweepy api.user_timeline <純 1 若吾ょ祉<純絲障若純若吾ф

for p in tweepy.Cursor(api.user_timeline,since_id=10000).items(100):
     print unicode(p.text)

筝箴ゃ若ID 1001 ゃ若違ゃ若障с<с違祉ゃ若 100 篁吟潟

若鴻潟鴻祉潟若

http://apiwiki.twitter.com/HTTP-Response-Codes-and-Errors 潟若筝荀

tweepy с若腮蕁

    try:
        r = api.get_status(id)
    except tweepy.error.TweepError, e:
        print "error response code: " + str(e.response.status)
        print "error messate: " + str(e.response.reason)

羂ャ

api.favorites(id=xxxxxxxx, page=xxxxxxx) c若吟羂ャゃ若緇с

_apitweepy.api.API
contributors
truncatedbool
textunicode
created_atdatetime.datetime
authortweepy.models.User
in_reply_to_status_id
coordinates
sourcestr
favoritedbool
place
in_reply_to_user_id
in_reply_to_screen_name
geo
idlong
usertweepy.models.User
罨≠
HOME


紕膣
薤藕

茯絖
沿
ユ茯


遵医