以下取自 http://www.plurk.com/API 之中的範例做些小小的調整
注意 api_key , 以及帳密的部份要改成自己的就可以測試POST訊息啦
其他的使用就請參考官網中的 Table of contents 的說明啦
#------------- code start ----------------
# -*- coding:utf-8 -*-
# 上面這一行一定要加, 否則一跑就出現Non Ascii的錯誤
# 修改自 http://www.plurk.com/API/ 中的python範例
import urllib, urllib2, cookielib
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
api_key = 'dKkIdUCoHo7vUDPjd3zE0bRvdm5a9sQi'
get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
encode = urllib.urlencode
#--- Requests ----------------------------------------------
fp = opener.open(get_api_url('/Users/login'),
encode({'username': 'user_x',
'password': 'user_x_pw',
'api_key': api_key}))
print fp.read()
# unicode 中文字
msg = u'中文字測試'
fp = opener.open(get_api_url('/Timeline/plurkAdd'),
encode({'content': msg.encode("utf8"),
'qualifier': 'says',
'lang': 'en',
'api_key': api_key}))
print fp.read()
# 針對中文字的處理, 僅需做encode("utf8")就可以解決了
#------------- code end ----------------
注意 api_key , 以及帳密的部份要改成自己的就可以測試POST訊息啦
其他的使用就請參考官網中的 Table of contents 的說明啦
#------------- code start ----------------
# -*- coding:utf-8 -*-
# 上面這一行一定要加, 否則一跑就出現Non Ascii的錯誤
# 修改自 http://www.plurk.com/API/ 中的python範例
import urllib, urllib2, cookielib
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
api_key = 'dKkIdUCoHo7vUDPjd3zE0bRvdm5a9sQi'
get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
encode = urllib.urlencode
#--- Requests ----------------------------------------------
fp = opener.open(get_api_url('/Users/login'),
encode({'username': 'user_x',
'password': 'user_x_pw',
'api_key': api_key}))
print fp.read()
# unicode 中文字
msg = u'中文字測試'
fp = opener.open(get_api_url('/Timeline/plurkAdd'),
encode({'content': msg.encode("utf8"),
'qualifier': 'says',
'lang': 'en',
'api_key': api_key}))
print fp.read()
# 針對中文字的處理, 僅需做encode("utf8")就可以解決了
#------------- code end ----------------
留言
張貼留言