【问题标题】:Requests Json throws an error in python 3.9请求 Json 在 python 3.9 中抛出错误
【发布时间】:2021-01-21 11:39:07
【问题描述】:

我正在运行以下代码,它在 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())

python中的错误如下:

Traceback(最近一次调用最后一次): 文件“”,第 13 行,在 json中的文件“/usr/lib/python3/dist-packages/requests/models.py”,第897行

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)

【问题讨论】:

标签: python python-3.7


【解决方案1】:

我敢打赌,您从 instagram 站点返回了一个错误,并且 JSON 解码器无法处理该消息(因为它不是 JSON)。试试

print(user_info.text)

查看网站返回的内容。

【讨论】:

  • 当我在 python 2.7 中运行代码时,它会准确地提取所有数据..我在 python 3.7 中运行 print(user_info.text) ..它抛出未登录的客户端根"跨度>
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2020-07-24
  • 2018-03-18
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多