对Weibo非官方Python SDK添加_Executable类,解决Python3发微博问题

对Weibo非官方Python SDK添加_Executable类,解决Python3发微博问题

洞庭小哥的文章:新浪微博Python SDK笔记——发微博(一)https://blog.csdn.net/dongtingzhizi/article/details/9098527 使用新浪官方SDK进行Python学习。
该文基于Python2所写。在使用非官方的3.0版SDK进行学习时,由于微博官方关闭了update方法,使用share方法进行替代,但执行时报"’‘function’ object has no attribute ‘share’ 错误"(如下图)。
在PY3.0环境中使用洞庭小哥PY2.7案例的client.statuses.share.post 的执行情况
经调试,在非官方版的3.0 SDK中,添加2.7版SDK的_Executable类,并将洞庭小哥原案例中的client.statuses.update.post 方法、print方法、raw_input方法更新后,即可正常通过微博API发帖。

一、洞庭小哥发微博案例更改点
1.将client.statuses.update.post 更改为 client.post.statuses__share,发帖的内容中记得加安全域名
2.print 打印内容用()括起来
3.使用input替代raw_input

修改后的发微博源代码,想看注释的可至洞庭小哥原帖

import weibo 
APP_KEY='185105**'
APP_SECERT='488134ca2ff45aee39bfacfd3a***'
CALL_BACK='https://api.weibo.com/****'

def run():
    client=weibo.APIClient(APP_KEY,APP_SECERT,CALL_BACK)
    auth_url=client.get_authorize_url()
    print "auth_url:\n" + auth_url
    
    code=raw_input("input the returned code:")
    r=client.request_access_token(code)
    client.set_access_token(r.access_token,r.expires_in)
    
    while True:
        print "Ready! Do you want to send a new weibo?(Y/N)"
        choice=raw_input()
        if choice=='y' or choice=='Y':
            content=raw_input('PLS input your new weibo content:')
            if content:
                client.statuses.share.post(status=content+"https://安全域名网址")
                print "Send succesfully!"
                break;
            else:
                print "Error!Empty content!"
        if choice=='n' or choice =='N':
            breakif __name__=="__main__":
        run()

二、非官方微博 Python 3.0 SDK修改点
在非官方PY3.0 SDK中,增加如下来自官方 SDK的代码,并保存为weibo.py,放置到python安装目录 Lib\site-packages下

class _Executable(object):

    def __init__(self, client, method, path):
        self._client = client
        self._method = method
        self._path = path
        
    def __call__(self, **kw):
        method = _METHOD_MAP[self._method]
        if method==_HTTP_POST and 'pic' in kw:
            method = _HTTP_UPLOAD
        return _http_call('%s%s.json' % (self._client.api_url, self._path), method, self._client.access_token, **kw)

    def __str__(self):
        return '_Executable (%s %s)' % (self._method, self._path)

    __repr__ = __str__

三、追加
在学习
新浪微博Python SDK笔记——发微博(二)https://blog.csdn.net/dongtingzhizi/article/details/9099795 中,若试图从本地保存了access_token的token-record.log读取token用于继续发帖时报如下错误,则将官方版sdk中的 JsonDict类的定义拷贝到 非官方版的 WEIBO PY3.0 SDK中即可

AttributeError: 'module' object has no attribute 'JsonDict'

同时如使用洞庭小哥的**发微博(二)**中的代码进行学习时问题较多,可以参考 https://github.com/Honghe/weiboanalyze 中的代码,该代码为某读者根据洞庭小哥的代码优化了处理逻辑后的结果。

WEIBO PY3.0非官方SDK 源代码见: http://www.cnblogs.com/txw1958/archive/2012/08/13/weibo-oauth2-python3.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值