strange python issue, 'unicode' object has no attribute 'read'

后端 未结 2 1276
误落风尘
误落风尘 2021-01-07 23:44

Here is my code and does anyone have any ideas what is wrong? I open my JSON content directly by browser and it works,

data = requests.get(\'http://ws.audios         


        
2条回答
  •  滥情空心
    2021-01-08 00:10

    requests.get(…).text returns the content as a single (unicode) string. The json.load() function however requires a file-like argument.

    The solution is rather simple: Just use loads instead of load:

    data = json.loads(data)
    

    An even better solution though is to simply call json() on the response object directly. So don’t use .text but .json():

    data = requests.get(…).json()
    

    While this uses json.loads itself internally, it hides that implementation detail, so you can just focus on getting the JSON response.

提交回复
热议问题