from time import sleep from selenium import webdriver from selenium.webdriver.chrome.service import Service import img_pdf_png def scroll_page(driver): # 设置滚动条缓慢滚动 page_height = driver.execute_script('return document.body.scrollHeight') # 页面高度 print(page_height) # web_height = driver.execute_script("return window.innerHeight") # 网页可见内容的高 原先用页面高度作为滚动条的滑动高度,会导致页面错乱 web_height = 300 # 网页可见内容的高 原先用页面高度作为滚动条的滑动高度,会导致页面错乱 n1 = page_height // web_height last_height = page_height % web_height print(last_height) driver.execute_script("document.documentElement.style.overflowY = 'hidden'") if n1 > 0: # 判断是否需要滚动 for j in range(n1): # print(r"j:{}".format(j)) if j == n1 - 1 and last_height > 0: # 截取尾部 driver.execute_script("window.scrollBy(0,{})".format(page_height)) # 滚动条滚到底 print("滚到底了") else: driver.execute_script("window.scrollBy(0,{})".format(web_height)) # print(web_height) sleep(1) new_page_height = driver.execute_script('return document.body.scrollHeight') # 页面高度 if new_page_height > page_height: scroll_page(driver) def html2img(driver, image_folder): page_width = driver.execute_script('return document.documentElement.scrollWidth') # page_width = 800 driver.execute_script("$('#app #tipsAlert').next().css('display', 'none')") # 隐藏置顶图标 window_height = driver.execute_script("return window.screen.height") # 屏幕高度 driver.set_window_size(page_width, window_height) # 设置浏览器宽高 scroll_page(driver) driver.execute_script("document.body.scrollTop = document.documentElement.scrollTop = 0") # 滚动条滚到底 page_height = driver.execute_script('return document.body.scrollHeight') # 页面高度 print("浏览器宽{} 浏览器高度{} 网页实际总高度{}".format(page_width, window_height, page_height)) web_height = driver.execute_script("return window.innerHeight") # 网页可见内容的高 原先用页面高度作为滚动条的滑动高度,会导致页面错乱 n1 = page_height // web_height print("当前窗口网页高度{}".format(web_height)) last_height = page_height % web_height print("底部多出来的部分{}".format(last_height)) margin_flag = False if n1 == 0 or (n1 == 1 and last_height == 0): # 判断是否需要滚动 driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0')) # 指定截图保存位置 else: j = 0 while j <= n1: if j == 0: driver.execute_script("window.scrollBy(0,0)") # print(0) elif j == n1 and last_height > 0: # 截取尾部 driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height)) driver.execute_script("window.scrollBy(0,{})".format(web_height)) # 滚动条滚到底 print("拉到底拉") else: driver.execute_script("window.scrollBy(0,{})".format(web_height)) sleep(1) driver.save_screenshot(r'{}/{}.png'.format(image_folder, j)) # 截屏 j = j + 1 # 调用截图函数 if __name__ == "__main__": # 创建一个 Chrome WebDriver 实例 options = webdriver.ChromeOptions() options.add_argument("headless") # options.add_argument(" window-size=1920,1080") # s = Service(executable_path='/Users/xiexiaoyuan/Downloads/chromedriver_mac64_111/chromedriver') s = Service(executable_path='E:/chromedriver-win64/chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) # driver.maximize_window() driver.get('https://ficc.hzinsights.com/reportshare_crm_report?code=4e38d30e656da5ae9d3a425109ce9e04') sleep(5) image_folder = './imgs' output_file = './screenshot' file_name = 'output1' html2img(driver, image_folder) img_pdf_png.merge_images(image_folder, output_file, file_name) driver.quit()