调整截图问题

This commit is contained in:
shikong 2024-03-02 16:15:32 +08:00
parent f2fc675d51
commit 05532dd278
6 changed files with 12 additions and 10 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
*.pyc *.pyc
screenshot.png screenshot.png
result.png result.png
screenshot.png

View File

@ -8,7 +8,7 @@ if __name__ == '__main__':
options.add_argument('lang=zh-CN') options.add_argument('lang=zh-CN')
options.add_argument( options.add_argument(
'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36') 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36')
# options.add_argument(r'--user-data-dir=E:\Repository\skcks.cn\python-selenium-spider\tmp\UserData') options.add_argument(r'--user-data-dir=E:\Repository\skcks.cn\python-selenium-spider\tmp\UserData')
options.add_argument('--disable-gpu') # 如果不加这个选项,有时定位会出现问题 options.add_argument('--disable-gpu') # 如果不加这个选项,有时定位会出现问题
# options.add_argument('--headless') # 增加无界面选项 # options.add_argument('--headless') # 增加无界面选项
@ -29,7 +29,7 @@ if __name__ == '__main__':
"source": f.read() "source": f.read()
}) })
run.run(driver=driver) run.main(driver=driver)
time.sleep(5) time.sleep(5)
finally: finally:
driver.quit() driver.quit()

View File

@ -20,7 +20,7 @@ if __name__ == '__main__':
driver = webdriver.Chrome(options=chrome_options) driver = webdriver.Chrome(options=chrome_options)
try: try:
run.run(driver=driver) run.main(driver=driver)
time.sleep(5) time.sleep(5)
finally: finally:
driver.close() driver.close()

View File

@ -6,10 +6,11 @@ from selenium.webdriver.remote.webdriver import WebDriver
import utils import utils
def run(driver:WebDriver): def main(driver:WebDriver):
driver.set_window_size(1920, 1080) driver.set_window_size(1920, 1080)
driver.get("https://bot.sannysoft.com/") # driver.get("https://bot.sannysoft.com/")\
driver.get("https://www.geetest.com/adaptive-captcha-demo")
width = driver.execute_script("return document.documentElement.scrollWidth") width = driver.execute_script("return document.documentElement.scrollWidth")
height = driver.execute_script("return document.documentElement.scrollHeight") height = driver.execute_script("return document.documentElement.scrollHeight")
print(width, height) print(width, height)
utils.screenshot(driver=driver, save_path="./screenshot.png") utils.screenshot(driver=driver, save_path="./tmp/geetest.png")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 315 KiB

After

Width:  |  Height:  |  Size: 315 KiB

View File

@ -22,7 +22,7 @@ def screenshot(driver:WebDriver, save_path):
current_h = driver.execute_script("return document.documentElement.clientHeight") current_h = driver.execute_script("return document.documentElement.clientHeight")
for i in range(1, int(body_h / current_h)): for i in range(1, int(body_h / current_h)):
driver.execute_script(_scroll_to_y % (current_h * i)) driver.execute_script(_scroll_to_y % (current_h * i))
time.sleep(0.2) time.sleep(0.5)
driver.save_screenshot(_tmp_file % (file_name, i)) driver.save_screenshot(_tmp_file % (file_name, i))
tmp_file_list.append(_tmp_file % (file_name, i)) tmp_file_list.append(_tmp_file % (file_name, i))
merge_images(tmp_file, _tmp_file % (file_name, i), tmp_file) merge_images(tmp_file, _tmp_file % (file_name, i), tmp_file)
@ -39,7 +39,7 @@ def screenshot(driver:WebDriver, save_path):
def merge_images(image1: str, image2: str,output: str,overlap_size=0): def merge_images(image1: str, image2: str,output: str,overlap_size=0):
size = overlap_size * 2 size = overlap_size
img1,img2 = Image.open(image1), Image.open(image2) img1,img2 = Image.open(image1), Image.open(image2)
size1, size2 = img1.size, img2.size size1, size2 = img1.size, img2.size
merge = Image.new("RGB", (size1[0], size1[1] + size2[1] - size)) merge = Image.new("RGB", (size1[0], size1[1] + size2[1] - size))