• pyppeteer获取图片base64


    首先尝试这么用:

    #     fulljs = """
    #         () => { return document.getElementsByClassName("geetest_canvas_fullbg")[0].toDataURL("image/png") }
    #         """
    #     fadejs = """
    #         () => { 
    #                 return document.getElementsByClassName("geetest_canvas_bg geetest_absolute")[0].toDataURL("image/png")}
    #         """

    会有异常 ailed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported

    TypeError: document.getElementsByClassName(...)[0].toDataURL is not a function

    看这一篇

    https://www.cnblogs.com/Renyi-Fan/p/9588755.html

    # -*- coding: utf-8 -*-
    import base64
    import os
    import sys
    import json
    import asyncio
    import time
    import numpy as np
    import cv2
    from pyppeteer import launch
    from PIL import Image, ImageChops
    import matplotlib.pyplot as plt
    
    
    async def main():
        browser = await launch(headless=False,autoClose=False, args=['--disable-infobars'])
        captcha_url = 'http://search.fang.com/captcha-40a1371b84c30293be/?t=1620803648.855&h=aHR0cHM6Ly9lc2YuZmFuZy5jb20vaG91c2UvaTMxNT9yZnNzPTItM2ExZjhlMDU0ZmJhMDE5MTQ5LTBi&c=cmE6MTEyLjEyLjE2LjIyO3hyaTo7eGZmOg%3D%3D#'
        
        page1 = await browser.newPage()
        await page1.goto(url=captcha_url)
    
    
        dimensions = await page1.evaluate(pageFunction='''() => {
            var canvas=document.createElement("canvas"),//获取canvas
                ctx = canvas.getContext("2d"), //对应的CanvasRenderingContext2D对象(画笔)
                img = new Image(),//创建新的图片对象
                base64 = '' ;//base64 
            img.src = document.getElementsByClassName("img-block")[0].src;
            img.setAttribute("crossOrigin",'Anonymous');
            img.onload = function(){//图片加载完,再draw 和 toDataURL
                ctx.drawImage(img,0,0);    
                base64 = canvas.toDataURL("image/png"); 
            };
            var dataURL=canvas.toDataURL("image/png");
            return {aa:dataURL}
            }''', force_expr=False)  # force_expr=False  执行的是函数
        print(dimensions)
    
    
     
    if __name__ == '__main__':
        asyncio.get_event_loop().run_until_complete(main())
  • 相关阅读:
    关于Vue的路由、脚手架笔记
    个人Vue-1.0学习笔记
    评论发布信息可插入QQ表情
    21 九宫格布局
    20 随机验证码&发表评论
    19 表单验证&上传图片验证
    18 百度换肤&千千音乐盒
    17 JS-图片切换PLUS&关闭小广告
    16 JS应用-图片切换&衣服相册
    15 JS应用-todolist任务
  • 原文地址:https://www.cnblogs.com/yansc/p/15576684.html
Copyright © 2020-2023  润新知