跳到主要內容

Python 使用 plurk API 的中文處理

以下取自 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 ----------------

留言

這個網誌中的熱門文章

Arduino 四軸飛行器開發實作,無人機硬體、無線遙控器、飛控軟體整合、飛行教學,一天學會

    您好: 2018/2/3(六)舉辦的「 Arduino 四軸飛行器實作坊 」,使用Arduino模組進行教學,非常容易學習,從無人機硬體、飛航控制到搖桿操控飛行,一天學會。 講師邀請到翔探科技團隊,自有技術研發的飛行器、原始程式碼、電路設計都是為了讓初學者能夠踏入四軸飛行器領域,套件亦適合進階應用開發。 ...

Where to find your coding community

How to make the most of Python communities; 3 new courses; beating automation Newsletter #48 May 10, 2018 Essential news, trends, and stories from Codecademy and the world of code. Learn Python with Friends & Mentors What would our school days have been without our best friends and favorite teachers? Being a student is more rewarding with a community to inspire us, hold us accountable, and push us forward. That's why we explained how to find and make the most of a supportive community while learning Python, or any other programming language. From in-person meetups to onlin...

這些,你都想要嗎?開信抽:陽明山天籟渡假酒店露天風呂體驗券、agnes b.托特包、麥當勞冰淇淋兌換券...

如您無法閱讀此信, 請點選此處看詳細內容。 06 Oct 親愛的臉書書友, 現在只要加入指定粉絲團 & 留言投票,就有機會獲得以下好禮喔: 免費送!你肯定喜歡的生活好禮 » 送【UCC下午茶禮券】名額10名! 送【商周集團 X 羅志祥Stage聯名T-Shirt】價值 $1800元... 送【HTC One M8 16G手機】價值 $17500元 即將結束!熱門活動通通倒數中 » 送【鑰匙掛飾一組】價值 $1680元 送【85度C商品兌換券】名額5名! ►更多禮物:mini30拍立得、Loqi春捲包、蕾絲鏡面珠寶盒… 特別推薦!一起按讚挖好康 » 送【Apple i-Pad Air】價值 $15900元 送:Hello Kitty鬧鐘、 Play Boy手錶、達菲熊伸縮識別夾... 送:全家500元禮券、OK超商1000元禮券、欣葉下午茶餐券2張... 用力省錢!超夯優惠直接享 » 【摩斯漢堡】早餐嚐鮮價 70元! 【漢堡王】著黑色服裝購買黑皮堡套餐享薯條飲料升級! ...