Python自动截图html页面

需求:实现自动截图html页面:

准备:
1、安装selenium
1)pip install selenium
2) 通过pycharm解释器图形化安装
File->settings->project interpreter

2、安装chromedriver
1)pip install chromedriver
2) 通过pycharm解释器图形化安装
File->settings->project interpreter

3、下载chromedriver.exe执行文件
查看自己chrome版本,然后下载对应版本的chromedriver.exe。地址如下:
http://chromedriver.storage.googleapis.com/index.html?path=77.0.3865.40/

4、实现代码

import os
import shutil
from selenium import webdriver
import time
import random

try:
    driver = webdriver.Chrome(r"E:\chromedriver.exe")          ## 自己现在并放到指定目录,需要自己修改
    picture_url = "https://www.baidu.com"

    driver.get(picture_url)
    driver.maximize_window()

    print(dir(driver))

    time.sleep(1)

    driver.get_screenshot_as_file('E:\\11.jpg')
    print("%s:截图成功!!!" % picture_url)
    driver.close()
except BaseException as msg:
    print(msg)

猜你喜欢

转载自www.cnblogs.com/Mongol-J/p/12712492.html