第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從tweepy異常實例獲取錯誤代碼

從tweepy異常實例獲取錯誤代碼

富國滬深 2021-03-30 15:32:38
我是python的新手,正在嘗試使用一個庫。它提出了一個例外,我正在嘗試確定哪個例外。這是我正在嘗試的:except tweepy.TweepError as e:    print e    print type(e)    print e.__dict__    print e.reason    print type(e.reason)這就是我得到的:[{u'message': u'Sorry, that page does not exist', u'code': 34}]<class 'tweepy.error.TweepError'>{'reason': u"[{u'message': u'Sorry, that page does not exist', u'code': 34}]", 'response': <httplib.HTTPResponse instance at 0x00000000029CEAC8>}[{u'message': u'Sorry, that page does not exist', u'code': 34}]<type 'unicode'>我正在嘗試獲取該代碼。我嘗試了e.reason.code,但沒有成功,我也不知道要嘗試什么。
查看完整描述

3 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

這個怎么樣?


except tweepy.TweepError as e:

    print e.message[0]['code']  # prints 34

    print e.args[0][0]['code']  # prints 34


查看完整回答
反對 回復 2021-04-02
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

要僅獲取錯誤代碼,請使用發(fā)布的monq方法。以下示例說明了如何同時獲取錯誤代碼和消息。我必須從e.reason字符串中提取消息,如果有人有更好的方法來僅檢索消息,請分享。


注意:此代碼應適用于以下格式的任何錯誤代碼/原因。


[{'code':50,'message':'找不到用戶。'}]


def getExceptionMessage(msg):

    words = msg.split(' ')


    errorMsg = ""

    for index, word in enumerate(words):

        if index not in [0,1,2]:

            errorMsg = errorMsg + ' ' + word

    errorMsg = errorMsg.rstrip("\'}]")

    errorMsg = errorMsg.lstrip(" \'")


    return errorMsg

您可以這樣稱呼它:


try:

    # Some tweepy api call, ex) api.get_user(screen_name = usrScreenName)

except tweepy.TweepError as e:

    print (e.api_code)

    print (getExceptionMessage(e.reason))


查看完整回答
反對 回復 2021-04-02
  • 3 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號