python實現(xiàn)自動網(wǎng)頁截圖并裁剪圖片
更新時間:2018年07月30日 14:11:49 作者:Erick-LONG
這篇文章主要為大家詳細介紹了python實現(xiàn)自動網(wǎng)頁截圖并裁剪圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python自動網(wǎng)頁截圖并裁剪圖片的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
# 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('跳轉(zhuǎn)到驗證碼頁面中...') 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('跳轉(zhuǎn)到創(chuàng)意編輯頁面中...') 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("創(chuàng)意--> {}-{}.png 已經(jīng)保存".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('所有抓取結(jié)束') crop_img() print('所有裁剪結(jié)束')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲功能示例
這篇文章主要介紹了Python實現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲功能,涉及Python針對網(wǎng)頁的獲取、字符串正則判定等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python matplotlib實戰(zhàn)之漏斗圖繪制
漏斗圖,形如“漏斗”,用于展示數(shù)據(jù)的逐漸減少或過濾過程,這篇文章主要為大家介紹了如何使用Matplotlib繪制漏斗圖,需要的小伙伴可以參考下2023-08-08Python中函數(shù)的創(chuàng)建與調(diào)用你了解嗎
這篇文章主要為大家詳細介紹了Python中函數(shù)的創(chuàng)建與調(diào)用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03Pygame實戰(zhàn)練習(xí)之炸彈人學(xué)院游戲
炸彈人學(xué)院想必是很多人童年時期的經(jīng)典游戲,我們依舊能記得抱個老人機娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個簡單的炸彈人學(xué)院小游戲的相關(guān)資料,需要的朋友可以參考下2021-09-09Python編程中用close()方法關(guān)閉文件的教程
這篇文章主要介紹了Python編程中用close()方法關(guān)閉文件的教程,是Python編程入門中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05