Python - 请求在输出中添加额外字符作为前缀

Python - requests is prefixing extra characters in the output

我正在使用 requests 模块来 运行 一个 curl 命令,不确定为什么它在输出前面加上前缀 ')]}\',这使得 r.json() 失败,因为如下所示。

当我在浏览器中粘贴 URL 并执行时,它会下载一个 .json 文件,并且该文件的前面有相同的字符,我是否缺少某些选项?我需要将输出处理为 json.

>>> r = requests.get('https://gerrit-review-server/a/changes/?q=status:open%20project:myproj/test/a_proj%20change:1510&o=CURRENT_REVISION', verify=False, auth=HTTPBasicAuth('user','pass'), 
>>> 

>>> r.text
')]}\'\n[{"id":"myproj%2Ftest%2Fa_proj~master~I15790ba05690e0a9984cb05bce06574645274966","project":"myproj/test/a_proj","branch":"master","hashtags":[],"change_id":"I15790ba05690e0a9984cb05bce06574645274966","subject":"Test","status":"NEW","created":"2021-01-27 19:38:57.000000000","updated":"2021-03-21 14:19:42.000000000","submit_type":"MERGE_IF_NECESSARY","mergeable":true,"insertions":1,"deletions":0,"total_comment_count":0,"unresolved_comment_count":0,"has_review_started":true,"_number":1510,"owner":{"_account_id":10008339},"current_revision":"fe3cc60cc66ad6f20c631ee818ccd91955c69d37",<..deleted..>,"description":"Rebase"}},"requirements":[]}]\n'

>>> r.json()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/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)
>>> 

正如Carcigenicate所说,似乎json的格式有误。您可以尝试加载,如果失败尝试更正:

r = requests.get(some_url) 

try:
    data = r.json()
except json.JSONDecodeError:
    # cut everything in front of the first "\n" 
    raw_data = r.text.split("\n", maxsplit=1)[1]
    # cut everything behind the last "\n"
    raw_data = raw_data.rsplit("\n", maxsplit=1)[0]
    # try to load again the json
    # If it fails it will raise the exception again
    data = json.loads(raw_data)