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

后端 未结 2 1211
误落风尘
误落风尘 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-07 23:59

    This error raised because the data is a unicode/str variable, change the second line of your code to resolve your error:

    data = json.loads(data)
    

    json.load get a file object in first parameter position and call the read method of this.

    Also you can call the json method of the response to fetch data directly:

    response = requests.get('http://ws.audioscrobbler.com/2.0/?method=library.getartists&api_key=4c22bd45cf5aa6e408e02b3fc1bff690&user=joanofarctan&format=json')
    data = response.json()
    

提交回复
热议问题