【发布时间】:2018-05-19 08:17:16
【问题描述】:
我正在尝试编写一个 python 脚本,它一次接收一堆 url 并获取该 url 的响应内容并将其存储为 json 文件。
这是我最初为获取 url 的响应而写的内容
def download_json()
params={'id':00163E0BD0C1FA89,
'list':'141',
'queue': 'gen',
'type': 'abc_stat'
}
req_obj= requests.get(link, params=params)
print(req_obj.url)
print(req_obj.status_code)
return req_obj
它会创建正确的 url,因为当我直接在浏览器中复制 url 时,它会以 json 格式显示输出。这是我在浏览器上看到的一行 json 输出:
{
"DATA" : [
{
"SCHEMA" : "abc_4_QAATu2.",
"ID" : "QAATu2",
"IM_ID" : "22faba86_c9e0_4dbc",
"S_NUMBER" : "502379284",
"CONFIG_TYPE" : "las_home_type",
"CONFIG_KEY" : "las_home_key",
"CONFIG_LONG_V" : "1",
"CONFIG_STRING_V" : "https://abc-deg/development",
"MODIFIED_DATE" : "Unknown"
},
所以这确实表明当我直接在浏览器中输入 url 时,数据以 json 格式返回。
但是我的请求对象有这个标题:
输出[26]:
{'content-length': '15457', 'expires': '0', 'content-encoding': 'gzip', 'cache-control': 'no-cache, no-store, private', 'set-cookie': 'login-XSRF_RZA=2018051-axJnifQUpOnrS8WCFI; path=/abc/deo/cpo; secure; HttpOnly, usercontext=client=002; path=/', 'content-type': 'text/html; charset=utf-8', 'pragma': 'no-cache, no-store, private'}
现在,当我执行 requests.json() 以获取 json python 对象中的数据时,出现以下错误
JSONDecodeError Traceback (most recent call last)
<ipython-input-28-4cfc1a694fcf> in <module>()
----> 1 req_obj.json()
/Users/anaconda/envs/dl/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs)
890 # used.
891 pass
--> 892 return complexjson.loads(self.text, **kwargs)
893
894 @property
/Users/anaconda/envs/dl/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
317 parse_int is None and parse_float is None and
318 parse_constant is None and object_pairs_hook is None and not kw):
--> 319 return _default_decoder.decode(s)
320 if cls is None:
321 cls = JSONDecoder
/Users/anaconda/envs/dl/lib/python3.5/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
/Users/anaconda/envs/dl/lib/python3.5/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 2 column 1 (char 2)
编辑:
如果您在上面的标题中看到的 content_type 显示为 html,即使在浏览器上它显示 json 作为输出
但是当我这样做时
req_obj.json
<bound method Response.json of <Response [200]>>
但是 req_obj.json() 给出以下错误。
知道为什么当输出实际上是如上所示的 json 格式时,它不能将数据转换为 json 格式吗?谢谢
【问题讨论】:
-
如果您使用
req_obj.text而不是req_obj.json()会发生什么?它输出什么? -
我得到了 html 输出。我不确定为什么在浏览器上我得到 json 输出时它给我 html 输出,如图所示。虽然在标题中 content_type 被提及为 html。我该如何改变呢?