溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖

發(fā)布時(shí)間:2021-03-23 15:31:38 來(lái)源:億速云 閱讀:492 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

所用工具和第三方庫(kù)

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()#最大化窗口

打開(kāi)網(wǎng)站

怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖 

我們可以看見(jiàn)一個(gè)ID為maincontent的元素,寬度為850PX,長(zhǎng)度為3828PX,這個(gè)長(zhǎng)度必須使用才能長(zhǎng)截圖才能完整截下來(lái)

el=b.find_element_by_id("maincontent")#找到元素

我們還需要一個(gè)重要的參數(shù),就是你電腦一次能截取多高的像素

先用下圖代碼獲取一個(gè)圖片

#fp為存放圖片的地址
b.get_screenshot_as_file(fp)

怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖 

也就是說(shuō)用我電腦上截圖的默認(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 = [] # 用來(lái)存放圖片地址

注釋:

1.count為元素的高度/每次截取的高度,比如這次實(shí)例中元素高度為3828PX,我每次截614px,需要6.2次,int之后變成6,也就是截6次,還剩一點(diǎn),那一點(diǎn)后面再說(shuō)

2.start_higth為初始高度,這個(gè)沒(méi)有什么可說(shuō)的

3.max_px為循環(huán)結(jié)束后,到達(dá)的高度

4.last_px為元素最底部的高度

5.surplus_px就是移動(dòng)6次后,還沒(méi)有截取的高度

屏幕每次移動(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)頁(yè)圖片,你可以打斷點(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)頁(yè)圖片
    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)

上面是把該元素的在頁(yè)面都截完,并且剪切,把圖片保存的路徑放入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)行效果圖:

怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖 

說(shuō)明完整的截取下來(lái)了

補(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)

效果如下:

怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖 

完整代碼:

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 = [] # 用來(lái)存放圖片地址
  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)頁(yè)圖片,你可以打斷點(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)頁(yè)圖片
    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)的大小,可以去截一張,去畫(huà)圖里面看看是多少像素,我這里是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)

上述內(nèi)容就是怎么在python中對(duì)元素進(jìn)行長(zhǎng)截圖,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI