用python实现对元素的长截图
2019-11-13 17:25
2491 查看
一.目标
浏览网页的时候,看见哪个元素,就能截取哪个元素当图片,不管那个元素有多长
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) # 执行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\test.png" # 图片地址,运行的话,改一下 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) # 元素的高度除以你每次截多少就是次数 start_higth = el.location["y"] # 元素的初始高度 max_px = start_higth + (count - 1) * sc_hight # for循环中最大的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) # 用于移动滑轮,每次移动614px,初始值是元素的初始高度 b.execute_script(js) # 执行js time.sleep(0.5) fp = r"C:\Users\wdj\Desktop\%s.png" % i # 图片地址,运行的话,改一下 b.get_screenshot_as_file(fp) # 屏幕截图,这里是截取是完整的网页图片,你可以打断点看一下图片 img = Image.open(fp=fp) img2 = img.crop((el.location["x"], 0, el.size["width"] + el.location["x"], sc_hight)) # 剪切图片 img2.save(fp) # 保存图片,覆盖完整的网页图片 img_path.append(fp) # 添加图片路径 time.sleep(0.5) print(js) else: js = "scrollTo(0,%s)" % last_px # 滚动到最后一个位置 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"])) # 创建一个新图片,大小为元素的大小 k = 0 for i in img_path: tem_img = Image.open(i) new_img.paste(tem_img, (0, sc_hight * k)) # 把图片贴上去,间隔一个截图的距离 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#你屏幕截图默认的大小,可以去截一张,去画图里面看看是多少像素,我这里是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 + Selenium 实现对页面的指定元素截图(可截长图元素)【转载】
- html2canvas根据DOM元素样式实现网页截图
- python两种方法实现从1000万个随机数中找出top n元素(附c语言版)
- mac使用python实现网页截图
- [算法Rust,Go,Python,JS实现)]LeetCode之27-移除元素
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现)
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现),比直接分开求更快速
- [053]python 递归实现list元素随机组合
- [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图
- Python代码实现删除一个list里面的重复元素
- Python实现截图
- Python实现具备元素唯一性的队列
- Python中使用 Selenium 实现网页截图实例
- 通过python调用adb命令实现用元素名称、id、class定位元素
- python实现从字典中删除元素的方法
- python实现判断数组是否包含指定元素的方法
- python,使用百度api实现复制截图中的文字
- 分别用的列表,递归,生成器三种方式创建出n个元素的斐波那契数列(Python3实现)
- selenium python 指定元素截图
- 求矩阵各个元素两两之间的欧式距离(python实现)