如何使用Python从受保护的画布元素获取图像?

2024-06-26 00:11:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从画布上得到一幅图像

(这是从https://newslibrary.naver.com/viewer/index.nhn?articleId=1995042000209101001&officeId=00020获取文章,但首先,单击“X”按钮查看文章。)

HTML代码是这样的

<p class="main_layer_article_body" data-container="">
    <canvas width="421" height="594" style="touch-action: none; cursor: inherit;">
    </canvas>
</p>

它的xpath是'//*[@id="_textContainer"]/p[2]/canvas'

根据类似问题的建议

  1. 我试着截图。但是,由于图片(文章)太长,我不得不向下滚动。因此,屏幕截图方法没有很好地工作

  2. 我试着直接从画布上保存图像

    canvas = driver.find_element_by_xpath('//*[@id="_textContainer"]/p[2]/canvas')
    canvas_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", canvas)
    output_image = base64.b64decode(canvas_base64)
    with open("image.png", 'wb') as f:
        f.write(output_image)
    

但它只返回一个全白色的图像,其大小约为6KB

有没有办法让我得到这幅油画(文章)的全貌


Tags: https图像imageidoutputpng画布driver