简体   繁体   English

请求 Json 在 python 3.9 中引发错误

[英]Requests Json throws an error in python 3.9

I am running the below code, it runs fine in python 2.7 but throws an error in python 3.7 or 3.9 compiler.我正在运行以下代码,它在 python 2.7 中运行良好,但在 python 3.7 或 3.9 编译器中引发错误。

import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

username = "geeks_for_geeks"

user_info = requests.get('https://instagram.com/%s/?__a=1'%username, headers = headers)

print (user_info.json())

The error in python is below: python 中的错误如下:

Traceback (most recent call last): File "", line 13, in File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json回溯(最后一次调用):文件“”,第 13 行,在文件“/usr/lib/python3/dist-packages/requests/models.py”中,第 897 行,在 json

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

I would bet that you are getting back an error from the instagram site and the JSON decoder can't process the message (because it is not JSON).我敢打赌,您从 instagram 站点收到了一个错误消息,并且 JSON 解码器无法处理该消息(因为它不是 JSON)。 try尝试

print(user_info.text)

to see what the site is returning.查看网站返回的内容。

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

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