使用python實(shí)現(xiàn)對(duì)元素的長(zhǎng)截圖功能
一.目標(biāo)
瀏覽網(wǎng)頁的時(shí)候,看見哪個(gè)元素,就能截取哪個(gè)元素當(dāng)圖片,不管那個(gè)元素有多長(zhǎng)
二.所用工具和第三方庫
python ,PIL,selenium
pycharm
三.代碼部分
長(zhǎng)截圖整體思路:
1.獲取元素
2.移動(dòng),截圖,移動(dòng),截圖,直到抵達(dá)元素的底部
3.把截圖按照元素所在位置切割,在所有圖片中只保留該元素
4.拼接
如果driver在環(huán)境變量中,那么不用指定路徑
b=webdriver.Chrome(executable_path=r"C:\Users\Desktop\chromedriver.exe")#指定一下driver b.get("https://www.w3school.com.cn/html/html_links.asp") b.maximize_window()#最大化窗口
打開網(wǎng)站
我們可以看見一個(gè)ID為maincontent的元素,寬度為850PX,長(zhǎng)度為3828PX,這個(gè)長(zhǎng)度必須使用才能長(zhǎng)截圖才能完整截下來
el=b.find_element_by_id("maincontent")#找到元素
我們還需要一個(gè)重要的參數(shù),就是你電腦一次能截取多高的像素
先用下圖代碼獲取一個(gè)圖片
#fp為存放圖片的地址 b.get_screenshot_as_file(fp)
也就是說用我電腦上截圖的默認(rèn)高度為614像素
所以我設(shè)置一個(gè)變量:
sc_hight=614
然后設(shè)置一下其他變量
count = int(el.size["height"] / sc_hight) # 元素的高度除以你每次截多少就是次數(shù) start_higth = el.location["y"] # 元素的初始高度 max_px = start_higth + (count - 1) * sc_hight # for循環(huán)中最大的px last_px = el.size["height"] + start_higth - sc_hight # 元素最底部的位置 surplus_px = last_px - max_px # 剩余的邊的高度 img_path = [] # 用來存放圖片地址
注釋:
1.count為元素的高度/每次截取的高度,比如這次實(shí)例中元素高度為3828PX,我每次截614px,需要6.2次,int之后變成6,也就是截6次,還剩一點(diǎn),那一點(diǎn)后面再說
2.start_higth為初始高度,這個(gè)沒有什么可說的
3.max_px為循環(huán)結(jié)束后,到達(dá)的高度
4.last_px為元素最底部的高度
5.surplus_px就是移動(dòng)6次后,還沒有截取的高度
屏幕每次移動(dòng),移動(dòng)sc_hight個(gè)像素,初始位置為(0,元素的Y值)
for i in range(0, count): js = "scrollTo(0,%s)" % (start_higth + i * sc_hight) # 用于移動(dòng)滑輪,每次移動(dòng)614px,初始值是元素的初始高度 b.execute_script(js) # 執(zhí)行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\%s.png" % i # 圖片地址,運(yùn)行的話,改一下 b.get_screenshot_as_file(fp) # 屏幕截圖,這里是截取是完整的網(wǎng)頁圖片,你可以打斷點(diǎn)看一下圖片 img = Image.open(fp=fp) img2 = img.crop((el.location["x"], 0, el.size["width"] + el.location["x"], sc_hight)) # 剪切圖片 img2.save(fp) # 保存圖片,覆蓋完整的網(wǎng)頁圖片 img_path.append(fp) # 添加圖片路徑 time.sleep(0.5) print(js) else: js = "scrollTo(0,%s)" % last_px # 滾動(dòng)到最后一個(gè)位置 b.execute_script(js) fp = r"C:\Users\wdj\Desktop\last.png" b.get_screenshot_as_file(fp) img = Image.open(fp=fp) print((el.location["x"], sc_hight - surplus_px, el.size["width"] + el.location["x"], sc_hight)) img2 = img.crop((el.location["x"], sc_hight - surplus_px, el.size["width"] + el.location["x"], sc_hight)) img2.save(fp) img_path.append(fp) print(js)
上面是把該元素的在頁面都截完,并且剪切,把圖片保存的路徑放入img_path
最后一步:把所有截圖都貼到新創(chuàng)建的圖片中
new_img = Image.new("RGB", (el.size["width"], el.size["height"])) # 創(chuàng)建一個(gè)新圖片,大小為元素的大小 k = 0 for i in img_path: tem_img = Image.open(i) new_img.paste(tem_img, (0, sc_hight * k)) # 把圖片貼上去,間隔一個(gè)截圖的距離 k += 1 else: new_img.save(r"C:\Users\wdj\Desktop\test.png") # 保存
運(yùn)行效果圖:
說明完整的截取下來了
補(bǔ)充優(yōu)化:
如果是個(gè)小元素怎么辦,不用長(zhǎng)截圖就能截取的那種
因?yàn)楹芎?jiǎn)單我就直接貼代碼了
start_higth = el.location["y"] js = "scrollTo(0,%s)" % (start_higth) b.execute_script(js) # 執(zhí)行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\test.png" # 圖片地址,運(yùn)行的話,改一下 b.get_screenshot_as_file(fp) img = Image.open(fp=fp) img2 = img.crop((el.location["x"], 0, el.size["width"] + el.location["x"], el.size["height"])) # 剪切圖片 img2.save(fp)
效果如下:
完整代碼:
from selenium import webdriver from PIL import Image import time def short_sc(el,b): start_higth = el.location["y"] js = "scrollTo(0,%s)" % (start_higth) b.execute_script(js) # 執(zhí)行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\test.png" # 圖片地址,運(yùn)行的話,改一下 b.get_screenshot_as_file(fp) img = Image.open(fp=fp) img2 = img.crop((el.location["x"], 0, el.size["width"] + el.location["x"], el.size["height"])) # 剪切圖片 img2.save(fp) def long_sc(el,b): count = int(el.size["height"] / sc_hight) # 元素的高度除以你每次截多少就是次數(shù) start_higth = el.location["y"] # 元素的初始高度 max_px = start_higth + (count - 1) * sc_hight # for循環(huán)中最大的px last_px = el.size["height"] + start_higth - sc_hight # 元素最底部的位置 surplus_px = last_px - max_px # 剩余的邊的高度 img_path = [] # 用來存放圖片地址 for i in range(0, count): js = "scrollTo(0,%s)" % (start_higth + i * sc_hight) # 用于移動(dòng)滑輪,每次移動(dòng)614px,初始值是元素的初始高度 b.execute_script(js) # 執(zhí)行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\%s.png" % i # 圖片地址,運(yùn)行的話,改一下 b.get_screenshot_as_file(fp) # 屏幕截圖,這里是截取是完整的網(wǎng)頁圖片,你可以打斷點(diǎn)看一下圖片 img = Image.open(fp=fp) img2 = img.crop((el.location["x"], 0, el.size["width"] + el.location["x"], sc_hight)) # 剪切圖片 img2.save(fp) # 保存圖片,覆蓋完整的網(wǎng)頁圖片 img_path.append(fp) # 添加圖片路徑 time.sleep(0.5) print(js) else: js = "scrollTo(0,%s)" % last_px # 滾動(dòng)到最后一個(gè)位置 b.execute_script(js) fp = r"C:\Users\wdj\Desktop\last.png" b.get_screenshot_as_file(fp) img = Image.open(fp=fp) print((el.location["x"], sc_hight - surplus_px, el.size["width"] + el.location["x"], sc_hight)) img2 = img.crop((el.location["x"], sc_hight - surplus_px, el.size["width"] + el.location["x"], sc_hight)) img2.save(fp) img_path.append(fp) print(js) new_img = Image.new("RGB", (el.size["width"], el.size["height"])) # 創(chuàng)建一個(gè)新圖片,大小為元素的大小 k = 0 for i in img_path: tem_img = Image.open(i) new_img.paste(tem_img, (0, sc_hight * k)) # 把圖片貼上去,間隔一個(gè)截圖的距離 k += 1 else: new_img.save(r"C:\Users\wdj\Desktop\test.png") # 保存 b=webdriver.Chrome(executable_path=r"C:\Users\wdj\Desktop\chromedriver.exe")#指定一下driver b.get("https://www.w3school.com.cn/html/html_links.asp") b.maximize_window()#最大化窗口 # b.get_screenshot_as_file(fp) sc_hight=614#你屏幕截圖默認(rèn)的大小,可以去截一張,去畫圖里面看看是多少像素,我這里是614像素 # b.switch_to.frame(b.find_element_by_xpath('//*[@id="intro"]/iframe')) el=b.find_element_by_id("maincontent")#找到元素 if el.size["height"]>sc_hight: long_sc(el,b) else: short_sc(el,b)
完整代碼
PS:
有些特殊情況,比如截取的元素在iframe中,直接用driver.switch_to.frame(iframe元素)
即可
或者不是iframe,但是元素有overflow屬性,直接用JS把他的overflow去掉就行
- python實(shí)現(xiàn)按鍵精靈找色點(diǎn)擊功能教程,使用pywin32和Pillow庫
- Python3 pywin32模塊安裝的詳細(xì)步驟
- Windows平臺(tái)Python編程必會(huì)模塊之pywin32介紹
- Python操作word常見方法示例【win32com與docx模塊】
- Python如何截圖保存的三種方法(小結(jié))
- python opencv 檢測(cè)移動(dòng)物體并截圖保存實(shí)例
- Python文字截圖識(shí)別OCR工具實(shí)例解析
- opencv python在視屏上截圖功能的實(shí)現(xiàn)
- 通過python實(shí)現(xiàn)windows桌面截圖代碼實(shí)例
- Python+Selenium+phantomjs實(shí)現(xiàn)網(wǎng)頁模擬登錄和截圖功能(windows環(huán)境)
- Python 通過截圖匹配原圖中的位置(opencv)實(shí)例
- 對(duì)Python獲取屏幕截圖的4種方法詳解
- python調(diào)用win32接口進(jìn)行截圖的示例
相關(guān)文章
python定時(shí)任務(wù)timeloop庫用法實(shí)例詳解
有些時(shí)候我們需要每隔一段時(shí)間就要執(zhí)行一段程序,或者是往復(fù)循環(huán)執(zhí)行某一個(gè)任務(wù),下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)timeloop庫用法的相關(guān)資料,需要的朋友可以參考下2023-01-01將python flask項(xiàng)目打包成可以運(yùn)行的軟件的全過程(包含報(bào)錯(cuò)解決)
這篇文章主要給大家介紹了將python flask項(xiàng)目打包成可以用運(yùn)行的軟件(包含報(bào)錯(cuò)解決),文中通過代碼示例和圖文結(jié)合講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02使用python批量修改文件名的方法(視頻合并時(shí))
這篇文章主要介紹了視頻合并時(shí)使用python批量修改文件名的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08如何用python獲取EXCEL文件內(nèi)容并保存到DBC
很多時(shí)候,使用python進(jìn)行數(shù)據(jù)分析的第一步就是讀取excel文件,下面這篇文章主要給大家介紹了關(guān)于如何用python獲取EXCEL文件內(nèi)容并保存到DBC的相關(guān)資料,需要的朋友可以參考2023-12-12利用python在Word文檔中創(chuàng)建和執(zhí)行條件郵件合并
郵件合并域和IF域是Word文檔中兩種非常實(shí)用的域,前者可以用來進(jìn)行郵件合并,根據(jù)數(shù)據(jù)批量創(chuàng)建定制的Word文檔,本文講介紹如何使用Python在Word文檔中創(chuàng)建條件郵件合并域以及執(zhí)行條件郵件合并,需要的朋友可以參考下2024-08-08Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能
Tornado是一個(gè)異步的Python Web開發(fā)框架,同時(shí)也是一個(gè)優(yōu)秀的異步服務(wù)器開發(fā)庫,這里我們將來講解一下Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能方面的一些重點(diǎn):2016-06-06