【问题标题】:Get the error code from tweepy exception instance从 tweepy 异常实例中获取错误代码
【发布时间】:2013-06-14 00:15:48
【问题描述】:

我是 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 没有成功,我不知道该尝试什么。

【问题讨论】:

  • 是的 - 我可能误读了那个 - e.reason[0]['code'] 怎么样?
  • 回溯(最近一次调用最后一次):文件“descargar.py”,第 70 行,在 print e.reason[0]['code'] TypeError: string indices must be integers
  • @alecxe 抱歉,这是前段时间,我不记得我做了什么,但我确实得到了代码(如我所愿)。不过,你的答案有效,所以我接受了:)

标签: python python-2.7 twitter tweepy


【解决方案1】:

从基异常类hasargs 属性(类型为tuple)派生的每个表现良好的异常都包含传递给该异常的参数。大多数情况下,只有一个参数被传递给异常,并且可以使用args[0] 访问。

Tweepy 传递给其异常的参数具有List[dict] 类型的结构。您可以使用以下代码从参数中获取错误代码(类型为int)和错误消息(类型为str):

e.args[0][0]['code']
e.args[0][0]['message']

TweepError exception class 还提供了几个额外的有用属性 api_codereasonresponse。出于某种原因,它们是 not documented,尽管它们是公共 API 的一部分。

因此您也可以使用此代码获取错误代码(键入int):

e.api_code


历史:

以前使用e.message[0]['code'] 访问的错误代码不再有效。 message 属性已为 deprecated in Python 2.6 并在 Python 3.0 中删除。目前您收到错误'TweepError' object has no attribute 'message'

【讨论】:

  • 谢谢你。我快疯了哈哈 文档真的过时了
  • e.api_code 返回None。至少在status code = 401 的时候,可能是个bug。
  • @j4n7 看看Twitter API documentation。如果我理解正确,每个响应都包含一个 HTTP 状态代码,但不是每个响应都包含一个错误代码(API 代码)。 API 代码仅针对最常见的错误提供。
  • 很奇怪。速率限制不在e.api_code 中。使用tweepy.RateLimit 异常。或e.args[0][0]['code']
  • 这怎么没有标记为最佳答案!谢谢。
【解决方案2】:

这个怎么样?

except tweepy.TweepError as e:
    print e.message[0]['code']  # prints 34
    print e.args[0][0]['code']  # prints 34

【讨论】:

  • 在我看来,因为e 是一个列表对象,所以e.message[0]['code'] 不起作用。你不会改用e[0]['code']吗?
  • 实际上,现在我查看了 github 页面,tweepError 正在用 __str__ 覆盖 print 语句,所以这可能就是我遇到问题的原因。你知道e 的结构吗?
  • e.message 不再起作用。请检查@Jeyekomon 答案
【解决方案3】:

自 2013 年以来情况发生了很大变化。目前的正确答案是使用e.api_code

【讨论】:

    【解决方案4】:

    要获取错误代码,请使用方法 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))
    

    【讨论】:

      【解决方案5】:

      这是我的做法:

      except tweepy.TweepError as e:
          print e.response.status
      

      【讨论】:

      • 真的打印出tweepy错误的代码吗(本例为34)?
      • 是的,如果发生错误,它会打印服务器返回的任何 HTTP 状态代码。
      • 但是,这里的 34 不是响应状态码,而是 twitter 错误码。
      • 我明白你的意思。在那种情况下,我的解决方案是不正确的。很抱歉造成混乱。
      【解决方案6】:

      截至 2021 年 12 月(tweepy v4.4.0)正确的方法是:

      except tweepy.TweepyException as e:
          print(e)
      

      来源:https://docs.tweepy.org/en/v4.4.0/changelog.html?highlight=TweepError#new-features-improvements

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 1970-01-01
        • 2020-04-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-13
        • 2020-03-20
        • 1970-01-01
        • 2011-08-08
        相关资源
        最近更新 更多