廢話不多說,先給大家看下python實現螢幕截圖的代碼,具體代碼如下所述:
from selenium import webdriverimport timedef capture(url, save_fn="capture.png"):browser = webdriver.Firefox() # Get local session of firefoxbrowser.set_window_size(1200, 900)browser.get(url) # Load pagebrowser.execute_script("""(function () {var y = 0;var step = 100;window.scroll(0, 0);function f() {if (y < document.body.scrollHeight) {y += step;window.scroll(0, y);setTimeout(f, 50);} else {window.scroll(0, 0);document.title += "scroll-done";}}setTimeout(f, 1000);})();""")for i in xrange(30):if "scroll-done" in browser.title:breaktime.sleep(1)browser.save_screenshot(save_fn)browser.close()if __name__ == "__main__":capture(http://www.jb51.net)
Pythony實現螢幕截屏的函數如下所述:
# -*- coding: cp936 -*- import time,Image import os, win32gui, win32ui, win32con, win32api def window_capture(dpath): ''''' 截屏函數,調用方法window_capture('d:\\') ,參數為指定儲存的目錄 返回圖片檔案名稱,檔案名稱格式:日期.jpg 如:2009328224853.jpg '''hwnd = 0hwndDC = win32gui.GetWindowDC(hwnd) mfcDC=win32ui.CreateDCFromHandle(hwndDC) saveDC=mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev=win32api.EnumDisplayMonitors(None,None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #圖片大小 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY) cc=time.gmtime() bmpname=str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp'saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname[:-4]+".jpg") os.remove(bmpname) jpgname=bmpname[:-4]+'.jpg'djpgname=dpath+jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen(copy_command) return bmpname[:-4]+'.jpg'#調用截屏函數 window_capture('d:\\')
以上所述是小編給大家分享的Python實現螢幕截圖的代碼及函數詳解,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的,在此有非常感謝大家對雲棲社區網站的支援!