美文网首页
python 接口返回的图片存储 base64转码

python 接口返回的图片存储 base64转码

作者: 孙宏志 | 来源:发表于2019-10-29 17:31 被阅读0次

    接到服务器返回:

    {"status":200,"statusText":"获取验证码成功","token":None,"body":{"img":"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a\nHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy\nMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAeAEYDASIA\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1uO9t\nZolliuI3ib7sisCh5xw3Tsao3NyDDC1zILd3Yxo0cyqS/Qr82O46AnOPpWRp0Etn4s1bTUkljE/+\nnwsTkH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/WtvwbtHhOwLAjP\nmYbkf8tGrWgsbGIXSpY2yfajmcpGFMpw3L8fN1PX1PqaswSwWtubSG1jhhwTEIlC7e/OAAff1+vN\nNRRSelkcl8PFQ6DPvcr/AKU3AbH8KUV1lnFbWUIW3tooYmwxWKMICxAycD8vwoprmWgSd3c//9k=","expire":120000,"token":"xxxxxx-xxxxxx-xxxxxx-xxxxxx"}}
    

    需获取图片并存储。
    需进行base64转码:

      import json,base64
    
    res = 上面的返回内容
    rest = json.dumps(res)
    res_json = json.loads(rest)
    
    body = res_json['body']
    img = body['img']
    print(img)
    with open("1.png","wb") as f:
        f.write(base64.b64decode(img))
    

    打开图片,能正确看到:

    1.png

    相关文章

      网友评论

          本文标题:python 接口返回的图片存储 base64转码

          本文链接:https://www.haomeiwen.com/subject/leufuctx.html