import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
import os
all_urls = [{'url':'url1', 'name':'pic1'},
{'url':'url2', 'name':'pic2'},
{'url':'url3', 'name':'pic3'},]
def login():
driver = webdriver.Chrome(executable_path='C:/Program Files/ntko plugin/chromedriver')
driver.set_window_size(1920, 1080)
print('initializing')
return driver
def get_screen(driver, urls):
count = 1
for url in urls:
print(url)
driver.get(url["url"])
print('screen shotting -> %s'% url)
count += 1
time.sleep(2)
driver.get_screenshot_as_file("./screen_shot/{}.png".format(url["name"]))
print("{}.png saved".format(url["name"]))
def crop_img():
for img in os.listdir('./screen_shot'):
if img.endswith('.png'):
print('%s cutting'% img)
im = Image.open('./screen_shot/%s'% img)
x = 320
y = 162
w = 920
h = 720
region = im.crop((x, y, x+w, y+h))
region.save("./screenshot_final/%s" % img)
if __name__ == '__main__':
driver = login()
get_screen(driver, all_urls)
driver.quit()
crop_img()
ps:
本文介绍如何利用Selenium和PIL库自动化获取网页截图,并进行精确裁剪,适用于批量处理多个网址的截图需求。通过Python脚本,设置Chrome浏览器参数,访问指定URL列表中的网页,自动保存截图到指定文件夹,最后使用PIL库裁剪图片并保存。
572

被折叠的 条评论
为什么被折叠?



