如何使用 python 将 base64 字符串转换并下载为来自 whatsapp 的图像?

How to convert and download base64 string to images from whatsapp using python?

我正在创建 whatsapp scraper 供我个人使用。我正在尝试从 html 代码下方下载图片:

<div class="_2n28r" style="background-image: url(&quot;data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABsbGxscGx4hIR4qLSgtKj04MzM4PV1CR0JHQl2NWGdYWGdYjX2Xe3N7l33gsJycsOD/2c7Z//////////////8BGxsbGxwbHiEhHiotKC0qPTgzMzg9XUJHQkdCXY1YZ1hYZ1iNfZd7c3uXfeCwnJyw4P/Zztn////////////////CABEIADAAQQMBIgACEQEDEQH/xAAwAAEAAgMBAAAAAAAAAAAAAAAEAAUBAgMGAQADAQEAAAAAAAAAAAAAAAABAgMABP/aAAwDAQACEAMQAAAAGZZo1nXnkFXJNleNWo3V1XBRCYqCxtvtyzi6787a9fOe0p7h1RFyD+LL3NG2c6TZXQOnVFlx56ww9hKKRb//xAAqEAACAQMDAQcFAQAAAAAAAAAAAQIDBBESITEzEBQiIzJBYgUTQlJhcf/aAAgBAQABPwC86pjYTlHhirVV+RCtUal/EK9qrlC+oJeqBSvaUzvFL9i86ou2y0Kfi4KlvS40Lgr0KS04XsRs4aMn2S86onsZEyg0t/kiclhl36qSIx8ETu0S86pnYyJlPOz+SHUxnLK8lKcN+GQeyFwi8fm9qZGeIxX9JV1uTrrUv9Kd3GelCl4Vv7F2/NMmTJJTklpTFGpN4SFbVs8FG2nHDlM758j/xAAdEQABBAIDAAAAAAAAAAAAAAABAAIDERATISJh/9oACAECAQE/AMRsBHK1Ba/cxDqE5XmORrWUjIDatf/EABsRAAICAwEAAAAAAAAAAAAAAAABAhEDEBIy/9oACAEDAQE/AFqbpnZ0LWT0IoWpxuRyUf/Z&quot;);">

但是当我清理 html 并获取 base 64 字符串并将其转换为图像时,我每次都得到损坏的图像 但是当我打印 base 64 字符串并将其粘贴到在线转换网站时,网站会完美地转换它

xx = driver.find_elements_by_class_name("_1iHeu")
d = 0
for m in xx:
    getList = m.find_element_by_class_name("_2kLly").find_element_by_class_name("_2n28r").get_attribute("style").split('url("')[1]
    d+=1
    

    if len(getList)<10:
        continue
    var = getList[0:len(getList)-3]
    
    result = base64.b64decode(str(var))
    
    content = result
    f1 = open("d"+'_'+str("d")+str(d)+'.png', 'wb')
    f1.write( content )
    f1.close()

这是我的代码片段。

为了方便起见,我也粘贴了完整的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time, base64, os, json, glob,io
import csv, requests, urllib, urllib.parse
import urllib.request as request
from PIL import Image


LAST_MESSAGES = 4
WAIT_FOR_CHAT_TO_LOAD = 5 # in secs

message_dic = {}
reject_list = []
driver = webdriver.Chrome("./chromedriver")
wait = WebDriverWait(driver, 10)
path = os.path.dirname(os.path.realpath(__file__))+os.sep
print(path)

total_chats = 0

driver.get("https://web.whatsapp.com/")
time.sleep(20)

def saveImage(name, img_cnt, ele, folder):
    global reject_list
    try:
        news = ele
        dm = news.get_attribute("src")
        dm = urllib.parse.unquote(dm)

        nm = img_cnt
        txt = dm#resp.content
        if txt.find('blob')!=-1:
            result = driver.execute_async_script("""
                var uri = arguments[0];
                var callback = arguments[1];
                var toBase64 = function(buffer){for(var r,n=new Uint8Array(buffer),t=n.length,a=new Uint8Array(4*Math.ceil(t/3)),i=new Uint8Array(64),o=0,c=0;64>c;++c)i[c]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(c);for(c=0;t-t%3>c;c+=3,o+=4)r=n[c]<<16|n[c+1]<<8|n[c+2],a[o]=i[r>>18],a[o+1]=i[r>>12&63],a[o+2]=i[r>>6&63],a[o+3]=i[63&r];return t%3===1?(r=n[t-1],a[o]=i[r>>2],a[o+1]=i[r<<4&63],a[o+2]=61,a[o+3]=61):t%3===2&&(r=(n[t-2]<<8)+n[t-1],a[o]=i[r>>10],a[o+1]=i[r>>4&63],a[o+2]=i[r<<2&63],a[o+3]=61),new TextDecoder("ascii").decode(a)};
                var xhr = new XMLHttpRequest();
                xhr.responseType = 'arraybuffer';
                xhr.onload = function(){ callback(toBase64(xhr.response)) };
                xhr.onerror = function(){ callback(xhr.status) };
                xhr.open('GET', uri);
                xhr.send();
                """, dm)
            if type(result) == int :
                raise Exception("Request failed with status %s" % result)
            result = base64.b64decode(result)
            
            content = result
            f1 = open(folder+os.sep+name+'_'+str(nm)+'.png', 'wb')
            f1.write( content )
            f1.close()
            pass
        
        elif txt.find('e=')!=-1:
            dm = urllib.parse.unquote(dm)
            ind = dm.find('e=')
            dm = dm[ind+2:]
            resp = requests.get(dm, headers={'User-Agent':'Mozilla Firefox'})
            f1 = open(folder+os.sep+name+'_'+str(nm)+'.png', 'wb')
            f1.write( resp.content )
            f1.close()
    except:
        reject_list.append( [name] )
        print("Unable to load data for images of profile ",name)

def getNewImages(ele):

    ele.click()
    time.sleep(5)

    script = "document.getElementsByClassName('_2y8MV')"

    get_data = driver.execute_script(
        """ 
        var abc = document.getElementsByClassName('_2y8MV')
        for (var i = 0;i<abc.length;i++){
        let ds = abc[i].innerText
        if (ds.indexOf("Docs")==17){
            abc[i].click()
        }
        
 
    }""")
    # var mxn = document.getElementsByClassName("_1iHeu")
    # for (var i = 0;i<mxn.length;i++){
    #     getdata = mxn[i]
    #     findData = getdata.firstChild.firstChild.getAttribute("style").split('url("')[1]
    #     if (findData==undefined){
    #         continue}
        
    #     var xx = findData.slice(findData.length,findData.length-2)
    #     console.log(findData)
    #     const linkSource = xx;
    #      const downloadLink = document.createElement("a");
    #      downloadLink.href = linkSource;
    #      downloadLink.download = "abc.png";
    #      downloadLink.click();
    # }
    #     """)
    # print(get_data)

    time.sleep(5)
    xx = driver.find_elements_by_class_name("_1iHeu")
    d = 0
    for m in xx:
        getList = m.find_element_by_class_name("_2kLly").find_element_by_class_name("_2n28r").get_attribute("style").split('url("')[1]
        d+=1
        

        if len(getList)<10:
            continue
        var = getList[0:len(getList)-3]
        
        result = base64.b64decode(str(var))
        
        content = result
        f1 = open("d"+'_'+str("d")+str(d)+'.png', 'wb')
        f1.write( content )
        f1.close()

    # Image = driver.find_elements_by_class_name("_275OX")
    # print(len(Image))

def chats():
    name1 =driver.find_element_by_xpath("//div[@class='DP7CM']/span") 
    name = driver.find_element_by_xpath("//div[@class='DP7CM']/span").text # profile name
    print("Checking chat of ",name)
    while name.find('/')!=-1:
        name = name.replace('/', ' ')
    folder = name[:]
    while folder.find(' ')!=-1 and folder.find('+')<2 and folder.find('+')!=-1:
        folder = folder.replace(' ','')
    folder = path+folder
    if not os.path.exists(folder):
        os.mkdir( folder )
    script = "document.getElementById('pane-side').scrollTo(5000,1)"
    # script = "document.getElementsByClassName('_2nmDZ')[0].scrollTo(5000, 1)"
    cnt =0
    ln = len( driver.find_elements_by_xpath("//div[@class='_3dtfX']") )
    # print(ln)
    while ln<3:
        driver.execute_script( script )
        time.sleep(2)
        cnt += 1
        ln = len( driver.find_elements_by_xpath("//div[@class='_3dtfX']") )
        if cnt>20 or ln>3:
            break

    # main = driver.find_element_by_id('main')
    # image = main.find_element_by_xpath("//img[@class='_39rvu _20GbR']")
    # print(image.get_attribute("src"))
    # message_dic[name].append(image.get_attribute('src'))
    
    # data = driver.find_elements_by_xpath('//div[@class="_2kLly"]')[0]
    # print(data)
    # get_images = data.find_elements_by_tag_name("img")
    # print(get_images)
    # nm = len( glob.glob(folder+os.sep+"*.png") )
    # for news in get_images:
    #     dm = news.get_attribute("src")
    #     if image.get_attribute('src')==dm or dm.find('data')!=-1:
    #         continue
    #     nm += 1
        saveImage(name, str(nm), news, folder)
    getNewImages(name1)




def scrape_Image():
    recentList = driver.find_elements_by_class_name("_210SC") 
    recentList.sort(key=lambda x: int(x.get_attribute('style').split("translateY(")[1].split('px')[0]), reverse=False)

    start = 0

    # script = "document.getElementById('pane-side').scrollTo(0,50000)"
    # driver.execute_script(script);
    
        
    for l in recentList[start:]:
    
        try:
            l.click()
            time.sleep(WAIT_FOR_CHAT_TO_LOAD)
            chats()
            next_focus = l
            
            # total_chats += 1
            
        except Exception as e:
            print( e )
            print("Error in checking next chat")


scrape_Image() 

如评论所述,您忘记删除代码中的“data:image/jpeg;base64,”。

如果您的 python 版本大于 3.4,您可以这样做:

from urllib.request import urlopen

getList = m.find_element_by_class_name("_2kLly").find_element_by_class_name("_2n28r").get_attribute("style").split('"')[1]
# now getList is "data:image/jpeg;base64,/9j/4AAQ..."

with urlopen(getList) as response, open('image.png', 'wb') as f:
    f.write(response.read())