簡體   English   中英

請求 Json 在 python 3.9 中引發錯誤

[英]Requests Json throws an error in python 3.9

我正在運行以下代碼,它在 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 中的錯誤如下:

回溯(最后一次調用):文件“”,第 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)

我敢打賭,您從 instagram 站點收到了一個錯誤消息,並且 JSON 解碼器無法處理該消息(因為它不是 JSON)。 嘗試

print(user_info.text)

查看網站返回的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM