其他分享
首页 > 其他分享> > pyppeteer获取图片base64

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())

 

标签:canvas,pyppeteer,img,base64,获取,toDataURL,import,document
来源: https://www.cnblogs.com/yansc/p/15576684.html