当前位置:   article > 正文

python 实现 浏览器页面转图片_python 根据html生成图片

python 根据html生成图片

python 实现 浏览器页面转图片

准备:需要下载chromedriver.exe (无头浏览器),放到你的xxxx.python 目录下

ChromeDriver 是 Chrome 驱动,是 Python 爬虫使用的 selenium 模块用来模拟打开谷歌浏览器所必须的一个文件,能模拟在谷歌浏览器上的操作。

python代码:


"""
将html页面转换为图片
"""
from sys import argv

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
import time

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--start-maximized')
path = os.getcwd()
filename = path + "/data/train/article/img/"

if __name__ == "__main__":
    # url = str(argv[1])
    start = time.perf_counter()  # 返回系统运行时间

    url = "http://XXXXXXXXX:8080/8300"

    driver = webdriver.ChromeOptions()
    driver = webdriver.Chrome(options=chrome_options)
    driver.get(url)
    # 将该页面的展开到最大
    width = driver.execute_script(
        "return Math.max(document.body.scrollWidth,document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
    height = driver.execute_script(
        "return Math.max(document.body.scrollHeight, document.body.offsetHeight,document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
    driver.set_window_size(width, height)

    driver.save_screenshot('C:/Users/omnipotent_wang/Desktop/a.png')
    driver.close()
    end = time.perf_counter()
    print('用时:{:.5f}s'.format(end - start))
    print('true')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

注意:如果你的访问不是私密连接,需要在上面加上一行代码

//这个是解决 Chrome 访问非受信证书页面时,提示「您的连接不是私密连接」错误的方法
chrome_options.add_argument('--ignore-certificate-errors')  
  • 1
  • 2

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/204567
推荐阅读
相关标签
  

闽ICP备14008679号