Python ValueError:无法解码 JSON 对象,但肯定 <Response [200]>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15883351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:15:10  来源:igfitidea点击:

ValueError: No JSON object could be decoded, but positive <Response [200]>

pythonjsonurl

提问by Arturo

I'm going over some URL's and I can fetch most of the data I can from an API I'm using. *Imgur API. However when it finds an image that has been posted before but was eventually removed it still shows a positive URL get response (code 200), and when I use

我正在浏览一些 URL,我可以从我正在使用的 API 中获取大部分数据。*Imgur API。但是,当它找到之前发布但最终被删除的图像时,它仍然显示一个积极的 URL get 响应(代码 200),当我使用

    j1 = json.loads(r_positive.text)

I get this error:

我收到此错误:

http://imgur.com/gallery/cJPSzbu.json
<Response [200]>
Traceback (most recent call last):
  File "image_poller_multiple.py", line 61, in <module>
    j1 = json.loads(r_positive.text)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

How can I "fetch" the error inside the j1 variable instead? I'd like to use a conditional structure to solve the problem and avoid my program from crashing. Something like

我怎样才能“获取” j1 变量中的错误?我想使用条件结构来解决问题并避免我的程序崩溃。就像是

if j1 == ValueError:
  continue
else:
  do_next_procedures()

采纳答案by Martijn Pieters

You need to use tryexceptinstead:

您需要tryexcept改用:

try:
    j1 = json.loads(r_positive.text)
except ValueError:
    # decoding failed
    continue
else:
    do_next_procedures()

See Handling Exceptionsin the Python tutorial.

请参阅Python 教程中的处理异常

What reallyhappens is that you were redirected for that URL and you got the image page instead. If you are using requeststo fetch the JSON, look at the response historyinstead:

什么真正的情况是,你被重定向该URL,你得到的图像页面,而不是。如果您使用的requests是获取 JSON,请查看响应历史记录

if r_positive.history:
    # more than one request, we were redirected:
    continue
else:
    j1 = r_positive.json()

or you could even disallowredirections:

或者你甚至可以禁止重定向:

r = requests.post(url, allow_redirects=False)
if r.status == 200:
    j1 = r.json() 

回答by 9000

The URL you listed redirects you to a HTML page. (Use curlto check things like this, he's your friend.)

您列出的 URL 会将您重定向到 HTML 页面。(curl用来检查这样的事情,他是你的朋友。)

The HTML page obviously cannot be parsed as JSON.

HTML 页面显然无法解析为 JSON。

What you probably need is this:

你可能需要的是这个:

response = fetch_the_url(url)
if response.status == 200:
  try:
    j1 = json.loads(response.text)
  except ValueError:
    # json can't be parsed
    continue