3 回答

TA貢獻1847條經驗 獲得超7個贊
這個怎么樣?
except tweepy.TweepError as e:
print e.message[0]['code'] # prints 34
print e.args[0][0]['code'] # prints 34

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))
添加回答
舉報