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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import os import json import time from urllib.parse import urlparse import os import time from PIL import Image import base64
from selenium import webdriver
def get_screenshot(url, width, height, timeout, real_time_out, host_dir, full_page): print("正在初始化浏览器") chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('lang=zh_CN.UTF-8') chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--disable-dev-shm-usage') chromedriver = "/usr/bin/chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(options=chrome_options, executable_path=chromedriver) print("正在尝试初始化窗口大小:", url) driver.set_window_size(width, height) print("正在获取网页") driver.get(url) print("正在等待网页加载完成") driver.implicitly_wait(timeout) time.sleep(real_time_out) print("获取网页成功,正在截图")
total_height = driver.execute_script("return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)")
if full_page != 0: now_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) pic_file = os.path.join(host_dir, now_time + ".png") if full_page == 3: print("|!!!!!|采用设备模拟截图模式") driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', {'mobile': False, 'width': width, 'height': total_height, 'deviceScaleFactor': 1}) res = driver.execute_cdp_cmd('Page.captureScreenshot', { 'fromSurface': True}) with open(pic_file, 'wb') as f: img = base64.b64decode(res['data']) f.write(img) driver.quit() return None
if full_page == 1: print("|!!!!!|采用拉高视窗截图模式") driver.set_window_size(width, total_height) driver.execute_script(f"window.scrollTo(0, {total_height});") else: print("|!!!!!|不进行任何操作,直接截图") driver.save_screenshot(pic_file) driver.quit() return None
print("|!!!!!|采用滚动截图模式") scrolled_height = 0 next_scrolled_height = 0 print("页面总高度:", total_height) now_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) image_path_list = [] page = 1 while next_scrolled_height < total_height: driver.execute_script(f"window.scrollTo(0, {next_scrolled_height});") next_scrolled_height += height if total_height - scrolled_height < height: next_scrolled_height = total_height print("正在截图:", scrolled_height, next_scrolled_height) time.sleep(2) pic_file = os.path.join(host_dir, now_time + "|" + str(scrolled_height) + "_"+ str(next_scrolled_height) + ".png") scrolled_height += height image_path_list.append(pic_file) driver.save_screenshot(pic_file) page += 1
_temp = [] for i in image_path_list: _temp.append(Image.open(i)) for i in range(len(_temp)): if i == range(len(_temp))[-1]: _temp[i] = _temp[i].crop((0, height - (int(image_path_list[-1].split("|")[-1].split(".")[0].split("_")[-1]) - int(image_path_list[-1].split("|")[-1].split(".")[0].split("_")[0])), _temp[i].width, height )) else: _temp[i] = _temp[i].crop((0, 0, _temp[i].width, height)) new_img = Image.new("RGB", (_temp[0].width, total_height)) for i in range(len(_temp)): new_img.paste(_temp[i], (0, i*height)) new_img.save(os.path.join(host_dir, now_time + ".png")) print("截图成功") driver.quit()
with open("list.json", "r") as f: data = json.load(f)
for i in data: url = i["url"] timeout = i["timeout"] width = i["width"] height = i["height"] real_time_out = i["real_time_out"] full_page = i["full_page"] host = urlparse(url).netloc host_dir = os.path.join("save", host) if not os.path.exists(host_dir): os.mkdir(host_dir) get_screenshot(url, width, height, timeout, real_time_out, host_dir, full_page)
|