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))
相关推荐
Bacon几秒前
给项目做一次"全身 CT":Agent Skills 生态里的代码体检全链路
前端·人工智能·后端
颜进强4 分钟前
微信小微AI-提审与发布|评测过了不等于能上线:四道前置条件,缺一道都白提交
前端·ai编程·全栈
颜进强5 分钟前
微信小微AI-【实战二】从零写一个 skill|卡片死活不出来?这 4 个坑我们全踩了一遍
前端·ai编程·全栈
颜进强8 分钟前
微信小微AI-命中效果调优|在 AI 的「选择题」里,怎么让小微更愿意选中你(系列收官)
前端·ai编程·全栈
颜进强9 分钟前
微信小微AI-【实战四】给 Agent 立人设|一份 AGENTS.md,决定小微是靠谱助手还是满嘴跑火车
前端·ai编程·全栈
颜进强10 分钟前
微信小微AI - 【实战一】工程化落地|Taro 主工程里塞进一套原生 AI 代码,我们这样让它们共存
前端·ai编程·全栈
颜进强16 分钟前
微信小微AI-【实战三】接力落地|用户点完 AI 卡片跨进小程序,那几秒才是体验的生死线
前端·ai编程·全栈
费曼学习法17 分钟前
为什么你的 Subagent 拆完反而更慢?我把 12 万 token 的任务重做了一遍
前端
颜进强22 分钟前
微信小微AI-接入模式与开通准备|两种模式选错,后面全白干——小微接入的隐藏门槛
前端·ai编程·全栈
htzyl20626 分钟前
CSS中,`:has()` 能帮你删掉的 4 类 JS
前端·css