这个脚本需要selenium来支持
主函数为cut_picture(url, save_path, sleep_time)
from time import sleep
from selenium import webdriver
def cut_picture(url, save_path, sleep_time: int = 2):
"""对网页进行截图的函数
:param url: 网页路径
:param save_path: 网页截图的存储路径
:param sleep_time: 网页等待时间【默认为两秒】
:return: 无返回值
"""
option = webdriver.ChromeOptions()
option.add_argument("headless")
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
sleep(sleep_time)
set_min_height(driver, 1900)
driver.save_screenshot(save_path)
driver.close()
def set_min_height(driver, w_width, pow_num: int = 10):
""" 得到浏览器的最小打开高度 【需要点时间】
:param driver: 浏览器对象
:param w_width: 浏览器打开宽度
:param pow_num: 指数检查的次数
:return: 返回最小高度
"""
pow_num = 10 if pow_num <= 0 else pow_num
w_height = pow(2, pow_num)
check_js = "return document.body.scrollHeight > " \
"(window.innerHeight || document.documentElement.clientHeight)"
while True:
flag = driver.execute_script(check_js)
if not flag:
break
w_height = w_height + pow(2, 10)
driver.set_window_size(w_width, w_height)
max_h = w_height
for i_end in range(1, pow_num):
to_num = pow(2, pow_num - i_end)
center_num = max_h - to_num
driver.set_window_size(w_width, center_num)
if not driver.execute_script(check_js): # 若中间值没有下拉的话
max_h = center_num
driver.set_window_size(w_width, max_h)
return max_h
# 下面的内容为调用的实例
cut_picture("https://aisuda.bce.baidu.com/amis/zh-CN/components/form/input-password", "截图.png")