您好,登錄后才能下訂單哦!
小編給大家分享一下python實現自動網頁截圖并裁剪圖片的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內容如下
代碼:
# coding=utf-8 import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from PIL import Image import os all_urls = ['http:/****edit'] def login(): chrome_options = Options() chrome_options.add_argument('--headless') driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options) driver.set_window_size(1200, 741) driver.implicitly_wait(2) print('初始化中...') driver.get("http://x*****e") print('填寫登錄信息中...') acc = driver.find_element_by_id('login-email') pwd = driver.find_element_by_id('login-pass') btn = driver.find_element_by_tag_name('button') acc.send_keys('***') pwd.send_keys('***') btn.click() print('跳轉到驗證碼頁面中...') time.sleep(2) capta = driver.find_element_by_id('code') capta_input = input('請輸入兩步驗證碼:') capta.send_keys(capta_input) btn1 = driver.find_element_by_tag_name('button') btn1.click() time.sleep(2) print('跳轉到創意編輯頁面中...') return driver def get_screen(driver,urls): count = 1 for url in urls: driver.get(url) print('正在抓取--> %s'% url) count +=1 time.sleep(2) uid = url.split('/')[-2] cid = url.split('/')[-5] driver.get_screenshot_as_file("./screen_shot/{}-{}.png".format(uid,cid)) print("創意--> {}-{}.png 已經保存".format(uid,cid)) print('還剩 %s 個'% str(len(urls)-count)) def crop_img(): for img in os.listdir('./screen_shot'): if img.endswith('.png'): print('%s裁剪中。。'% img) im = Image.open('./screen_shot/%s'% img) x = 755 y = 162 w = 383 h = 346 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() print('所有抓取結束') crop_img() print('所有裁剪結束')
看完了這篇文章,相信你對“python實現自動網頁截圖并裁剪圖片的方法”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。