【爬虫】data: image/png; base64图片数据

爬wx小程序时遇到了响应返回下面的信息

{"Image":"data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAeAEADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3uCNoreON5nmdECtLIFDOQPvHaAMnrwAPQCiGFYEKIXILs/zuznLEseSScZPA6AYAwABXIPruqr411Xw/e6np1pbnTTeWUkEB+0KpJUs252XKEZ+7hs9sEVZ07X9Z0+xjm8WWtrDDL8yXtgkpjjXr+/QgmHC8lizINrbmX5dz1auD0djZ1bWbLSIM3N9YW9xIrfZ0vLpYFlYYAG45OMlQSAcZHB6UWOpWOt288G0eYg8u8sZwvmQkjlJEyRyM4PKsOVLKQTxOix3etfEbxGLm+0rUNJlsrPdH9iLpNbuJWQKTKV75JwwbPAWutvdOlsbh9R0mBXZwBd2IIVbpQAAVzgLKFAAJwGACMQAjRroPZ2NLzI76E/ZbzASUBngZW5VhuQ5BHOCp7gE4IOCLFef6HeajZ+GtTvtHgs0sra8uZVt54njd0DklQPl8ohRgAqeeCFxXYxXi6hof2wWMsqTQl1tW2FpVIOF5bb8w6ZI6845xKd1934mtSlyPfS9i3DMs6F0DgB2T542Q5UlTwQDjI4PQjBGQQakriPDPhvQ72z1dLnSLGQDUrmJSYFyqBsAKcZAHbHSrfhWW8t9b1rRHumns9PkTyWlBaQCRQyrvJ6LgjkE8jkYxQpXt5q450Uuaz+H/AIYu674b03XNT0yeeQQ3trL5ymMgPNEoKtGe5T95z9fet+iiqMHrqchpnhjUtD8T6xr0d5aXEN8uX0+10/ynYrvZdrmbbvZnJYsMMST8ua6+o4zMXlEiIqB8RFXLFlwOSMDac7hgZ4AOecCSnsrB1ucePC2rQ6jqVtb6mn9hanL5s8T586IuHMnlnGAGPljHoWI2sCX6sReVa+TbbI9ibY8rlVwMDjIyPbIqWipsi5VJS3OWt/Des2tpdW8Ot2qC4umuy62Uisrlg3BEw4yBwcgjIOQSK19G0W30WCZYpJZpriUzTzzEF5XPc4AA9AAAK0qKFFLYcqs5Xv1P/9k=","Keyword":"dcc1a5fd373947e4adf7d735f263a6be"}

经过查阅资料发现,data:image/png;base64, 字符串…其实是Data URI scheme,URI表示统一资源标识符uniform resource identifier,字符串表示的是经过base64编码的图片数据

通常遇到的图片是如下形式,称为Http URI scheme

<img src="http://sjolzy.cn/images/A.jpg"/>

base64和图片的转化的在线网站
http://www.yzcopen.com/img/imgbase64


Python实现图片的base64转换和解析

import base64

# 图片转base64,返回base64编码结果
def ImageToBase64(img_path):
    with open(img_path,"rb") as f:#转为二进制形式读取图片
        base64_data = base64.b64encode(f.read())#使用base64进行加密
    return base64_data
    
# base64转图片并保存,imgStr是base64字符串,imgName文件名
def Base64ToImage(imgStr,imgName):
    imgdata = base64.b64decode(imgStr)
    with open(imgName,'wb') as fp:
        fp.write(imgdata)
        print(imgName + "保存成功!!!")

你可能感兴趣的:(爬虫)