[Python] 纯文本查看 复制代码 from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import pyautogui
from PIL import Image
def capture_element_screenshot(element1, mc):
# 获取元素的位置和大小
location = element1.location
size = element1.size
# 截取整个页面的屏幕截图
driver.save_screenshot("screenshot.png")
# 打开截图并裁剪出元素区域
x = location['x']
y = location['y']
width = size['width']
height = size['height']
im = Image.open('screenshot.png')
im = im.crop((int(x), int(y), int(x + width), int(y + height)))
im.save(f'D:/element_screenshot{a}_{mc}.png')
a = input("请输入要截取的页码:")
driver = webdriver.Edge()
driver.maximize_window()
driver.get("https://wqbook.wqxuetang.com/deep/read/pdf?bid=3225892")
sleep(10)
ele = driver.find_element(By.XPATH, "//*[@id='pageImgBox" + a + "']/div[3]")
driver.execute_script("arguments[0].scrollIntoView();", ele)
sleep(1)
ele1 = driver.find_element(By.XPATH, "//*[@id='pageImgBox" + a + "']/div[1]")
# 调用函数,截取元素屏幕截图
capture_element_screenshot(ele1, "1")
# 移动鼠标到指定位置
pyautogui.moveTo(500, 500, duration=1)
# 模拟鼠标点击
pyautogui.click()
sleep(1)
for i in range(8):
# 模拟按下pagedown键
pyautogui.press('down')
sleep(0.2)
ele1 = driver.find_element(By.XPATH, f'//*[@id="pageImgBox{a}"]/div[1]')
# 调用函数,截取元素屏幕截图
capture_element_screenshot(ele1, "2")
sleep(1)
# ele1 = driver.find_element(By.XPATH, '//*[@id="pageImgBox2"]/div[3]')
# driver.execute_script("arguments[0].scrollIntoView();", ele1)
sleep(1)
# 打开图片
img1 = Image.open('D:/element_screenshot' + a + '_1.png')
img2 = Image.open('D:/element_screenshot' + a + '_2.png')
# 截取图片1的前755像素的内容
area = (0, 0, img1.width, 755)
cropped_img1 = img1.crop(area)
# 将截取的部分替换到图片2的相应位置
img2.paste(cropped_img1, (0, 0))
# 保存结果
img2.save('D:/result' + a + '.png')
driver.quit()
|