【发布时间】:2020-08-07 08:32:48
【问题描述】:
当我使用以下命令时,我会从 url 获得正确的 json 响应:
curl --user user:password -H Accept:application/json -H Content-Type:application/json -X GET <url>
“正文”:{ “项目”: [ {........}]}
但是,当我尝试使用 python 请求模块做同样的事情时
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
user='admin'
password='yanQE3B'
url=<requestUrl>
response = requests.get(url, auth=(user, password) , verify=False)
print ( response.content ) ## prints html content
print ( response.text ) ## prints html content
data = response.json() ## error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
-
当我执行 request.json() 时,我得到一个错误 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
-
当我打印 request.content - 它返回带有 html 内容的 html 列表。 reponse.text 和 response.content 的输出是一样的。大量 li 标签被截断的 html 被返回:
<!DOCTYPE html>
<html>
<head>
<title>Data</title>
</head>
<div>
<ul>
<li>
body
<ul>
<li>items <ol>
<li>
<ul>
<li>name ........ </li><li>messages <ol>
</ol></li></ul></div></body></html>
【问题讨论】: