简体   繁体   English

Python请求:requests.get(url).json()错误

[英]Python Requests: requests.get(url).json() Error

When trying to use python requests I obtain the following: when I try to ensure a request is in json format. 当尝试使用python请求时,我获得以下内容:当我尝试确保请求是json格式时。 The request when using requests.get(url) is <Response [200]> . 使用requests.get(url)时的requests.get(url)<Response [200]>

However when using request.get(url).json() I obtain the following: 但是,当使用request.get(url).json()我获得以下内容:

  File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Printing requests.get(url) .headers gives the following: 打印requests.get(url) .headers给出以下内容:

{'Server': 'nginx/1.10.3 (Ubuntu)', 'Date': 'Mon, 01 Apr 2019 21:03:28 GMT', 'Content-Type': 'text/html', 'Last-Modified': 'Mon, 25 Mar 2019 21:25:01 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'ETag': 'W/"5c99472d-806"', 'Strict-Transport-Security': 'max-age=63072000; includeSubdomains', 'X-Frame-Options': 'DENY', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip'}

I've tried snipping certain characters like <, >, \\, \\0 but that didn't affect the outcome. 我已经尝试剪掉某些字符,如<,>,\\,\\ 0但这并没有影响结果。

以下请求应该有效。

import requests requests.get(URL, headers={'content-type':'application/json'}).json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM