js 请求blob:https:// 图片

方式1

复制代码
def get_file_content_chrome(driver, uri):
    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();
        """, uri)
    if type(result) == int:
        raise Exception("Request failed with status %s" % result)
    return base64.b64decode(result)
复制代码
bytes_data = get_file_content_chrome(driver, "blob:https://amazon-api.arkoselabs.com/8784ddf9-bbd2-48a6-b529-fc2bfcdaaebc")
# print('bytes_data:', bytes_data)
base64data0 = base64.b64encode(bytes_data).decode("utf8")
print('base64data0:', base64data0)
with open(f'./captcha0.jpg', 'wb') as file:
    file.write(bytes_data)

方式2

复制代码
js_img = '''function getImageBlob(url, cb) {
    var xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.responseType = "blob";
    xhr.onload = function() {
        if (this.status == 200) {
            if(cb) cb(this.response);
        }
    };
    xhr.send();
}
function preView(url){
    let reader = new FileReader();
    getImageBlob( url , function(blob){
        reader.readAsDataURL(blob);
    });
    reader.onload = function(e) {
        img = document.querySelector('img#captchaimg')
        if(img){
            img.src = e.target.result;
            document.body.appendChild(img);
        }else{
            var img = document.createElement("img");
            img.id = 'captchaimg'
            img.src = e.target.result;
            document.body.appendChild(img);
        }
    }
}
preView("blob_image")'''
driver.execute_script(js_img.replace('blob_image', blob_image))
time.sleep(2)
captchaimg = driver.find_element(By.XPATH, '//img[@id="captchaimg"]').get_attribute('src')
print('captchaimg:', captchaimg)
base64data = captchaimg.split('base64,')[1]
with io.BytesIO(b64decode(base64data)) as image_file:
    image = Image.open(image_file)
    image.save('./captcha.jpg')
content_to_bytes = bytes(captchaimg.split('base64,')[1], 'utf-8')
with open(f'./captcha1.jpg', 'wb') as file:
    file.write(base64.decodebytes(content_to_bytes))
相关推荐
掘金挖土19 分钟前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
中文呀3 小时前
三维 GIS 离线缓存实战:逆向 CesiumOfflineCache,并亲手为它补上"自动更新"能力
前端
前进前进前进3 小时前
TRAE 全链路实战:做了一个警示牌一键生成器:纯 Canvas、单文件、零依赖
前端
小万7424 小时前
God‘s Eye View 深度解析:15 路公开信号 + CesiumJS 的浏览器级 OSINT 控制台,60 帧渲染的“真实间谍卫星模拟器”
前端
IT星宿4 小时前
浏览器标签页全是一个图标:我用名字哈希生成 favicon,让每个项目都有身份标识
前端
OpsEye5 小时前
大模型账单对账难:不同厂商 Token 统计口径差异怎么处理
javascript·ai编程
全栈项目管理程序猿5 小时前
ArcGIS JS 基础教程(32):体元系列 - VoxelSlice 体元切片
前端·javascript
拖孩7 小时前
AI 帮我写的小程序「厨菜记」上线满月了,挣了两百块!!!
前端·后端·微信小程序
用户360055579007 小时前
Kestrel_VLLM - 13 · 为什么排除 MoE FFN Tiling 与 Tensor Core
前端
Hilaku7 小时前
AI 流式输出都用 SSE,什么时候必须换 WebSocket?
前端·javascript·程序员