此链接为新浪微博开放的Python SDK http://code.google.com/p/sinatpy/downloads/list
关于新浪微博应用开发简介(认证及授权部分)请参考这篇文章,讲的非常详细, http://blog.csdn.net/dyx1024/article/details/7427776
关于通过Python客户端发送微博,请参考这篇文章,http://blog.csdn.net/dyx1024/article/details/7237823
发布微博的部分略加完善可参照如下代码修改:
while True:
# 通过命令行输入要发布的内容
weibo_content = raw_input('Please input content:')
if not weibo_content:
print u"发布微博结束...".encode('gb2312')
break
# 转为unicode格式,如 u'中国',就可以支持中文了
try:
status = api.update_status(status=unicode(weibo_content,'gbk'))
except weibopy.error.WeibopError:
print u"发送微博失败,相同的微博不要发送多次哦".encode('gb2312')
print "Press sina weibo successful,content is:%s" % status.text.encode('gb2312')关于通过Python客户端上传图片至新浪微博,请参考这篇文章,
http://blog.csdn.net/dyx1024/article/details/7246830
当运行上述代码时,程序会抛出异常: sequence item o:expected string, int found
经过查询,Python中如果join方法参数里包含数字便会raise这种异常,经仔细跟踪代码,发现在Python SDK的 api.py ,_pack_image 方法内887-890行:
headers = {
'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
'Content-Length': len(body)
}请参考如下修改代码:
headers = {
'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
'Content-Length': str(len(body))
}编译便可通过,可成功发送带图片的微博。
关于Oauth验证的介绍,请参考这篇文章,讲述的非常详细,http://blog.csdn.net/hereweare2009/article/details/3968582
另外再提供关于Oauth的几个官方站点:http://oauth.net/
新浪微博官方网站上的:http://open.t.sina.com.cn/wiki/index.php/Oauth_new
本文介绍了如何使用Python结合新浪提供的SDK,进行微博客户端的开发。内容包括通过Python SDK发送文字和图片微博,解决join方法参数中出现数字导致的异常问题,以及OAuth验证的原理和相关资源链接。

384

被折叠的 条评论
为什么被折叠?



