|
@@ -3,6 +3,8 @@ from time import sleep
|
|
|
from selenium import webdriver
|
|
|
from selenium.webdriver.chrome.service import Service
|
|
|
import img_pdf_png
|
|
|
+import os
|
|
|
+from PIL import Image
|
|
|
|
|
|
|
|
|
def scroll_page(driver):
|
|
@@ -19,7 +21,7 @@ def scroll_page(driver):
|
|
|
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)) # 滚动条滚到底
|
|
|
+ driver.execute_script("window.scrollBy(0,{})".format(last_height)) # 滚动条滚到底
|
|
|
print("滚到底了")
|
|
|
else:
|
|
|
driver.execute_script("window.scrollBy(0,{})".format(web_height))
|
|
@@ -45,26 +47,95 @@ def html2img(driver, image_folder):
|
|
|
print("当前窗口网页高度{}".format(web_height))
|
|
|
last_height = page_height % web_height
|
|
|
print("底部多出来的部分{}".format(last_height))
|
|
|
- margin_flag = False
|
|
|
+
|
|
|
+ is_end = 0
|
|
|
if n1 == 0 or (n1 == 1 and last_height == 0): # 判断是否需要滚动
|
|
|
- driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0')) # 指定截图保存位置
|
|
|
+ # 去除留白部分, 一般情况下也不会只写一页报告且留那么多空白
|
|
|
+ crop_height = web_height - page_height
|
|
|
+ if crop_height > 0:
|
|
|
+ origin_last_img = r'{}/{}.png'.format(image_folder, 1000)
|
|
|
+ new_last_img = r'{}/{}.png'.format(image_folder, 0)
|
|
|
+ driver.save_screenshot(origin_last_img)
|
|
|
+ crop_img_botton(origin_last_img, new_last_img, page_height)
|
|
|
+ else:
|
|
|
+ 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)) # 滚动条滚到底
|
|
|
+ elif j == n1:
|
|
|
+ # print(f'n1 is {n1}')
|
|
|
+ if last_height > 0:
|
|
|
+ # driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
|
|
|
+ driver.execute_script("window.scrollBy(0,{})".format(last_height)) # 滚动条滚到底
|
|
|
+
|
|
|
+ # 最后一屏先进行一次保存
|
|
|
+ sleep(1)
|
|
|
+ origin_last_img = r'{}/{}.png'.format(image_folder, j + 1000)
|
|
|
+ driver.save_screenshot(origin_last_img)
|
|
|
+
|
|
|
+ # 截取上面重叠的一部分并保存截取后的图片
|
|
|
+ new_last_img = r'{}/{}.png'.format(image_folder, j)
|
|
|
+ crop_height = web_height - last_height
|
|
|
+ crop_img_top(origin_last_img, new_last_img, crop_height)
|
|
|
+
|
|
|
+ is_end = 1
|
|
|
print("拉到底拉")
|
|
|
else:
|
|
|
driver.execute_script("window.scrollBy(0,{})".format(web_height))
|
|
|
- sleep(1)
|
|
|
- driver.save_screenshot(r'{}/{}.png'.format(image_folder, j)) # 截屏
|
|
|
+
|
|
|
+ if is_end == 0:
|
|
|
+ sleep(1)
|
|
|
+ driver.save_screenshot(r'{}/{}.png'.format(image_folder, j)) # 截屏
|
|
|
j = j + 1
|
|
|
|
|
|
|
|
|
+# crop_img_top 裁掉图片上面的部分保留剩下的部分
|
|
|
+def crop_img_top(image_path, output_path, crop_height):
|
|
|
+ # 打开图像文件
|
|
|
+ img = Image.open(image_path)
|
|
|
+
|
|
|
+ # 获取图像的宽度和高度
|
|
|
+ width, height = img.size
|
|
|
+
|
|
|
+ # 计算裁剪的起始坐标
|
|
|
+ start_x = 0
|
|
|
+ start_y = min(height, crop_height) # 保证不超出图像下边界
|
|
|
+
|
|
|
+ # 裁剪图像,仅保留上半部分
|
|
|
+ cropped_img = img.crop((start_x, start_y, start_x + width, start_y + (height - crop_height)))
|
|
|
+
|
|
|
+ # 保存裁剪后的图像
|
|
|
+ cropped_img.save(output_path)
|
|
|
+
|
|
|
+ # 删除原始图像
|
|
|
+ os.remove(image_path)
|
|
|
+
|
|
|
+
|
|
|
+# crop_img_botton 裁掉图片下面的部分保留上面的部分
|
|
|
+def crop_img_botton(image_path, output_path, keep_height):
|
|
|
+ # 打开图像文件
|
|
|
+ img = Image.open(image_path)
|
|
|
+
|
|
|
+ # 获取图像的宽度和高度
|
|
|
+ width, height = img.size
|
|
|
+
|
|
|
+ # 计算保留部分的起始坐标
|
|
|
+ start_x = 0
|
|
|
+ start_y = 0
|
|
|
+
|
|
|
+ # 裁剪图像,保留从左上角开始指定高度的部分
|
|
|
+ cropped_img = img.crop((start_x, start_y, start_x + width, start_y + keep_height))
|
|
|
+
|
|
|
+ # 保存裁剪后的图像
|
|
|
+ cropped_img.save(output_path)
|
|
|
+
|
|
|
+ # 删除原始图像
|
|
|
+ os.remove(image_path)
|
|
|
+
|
|
|
+
|
|
|
# 调用截图函数
|
|
|
if __name__ == "__main__":
|
|
|
# 创建一个 Chrome WebDriver 实例
|