Python实现网页截图(PyQT5)
方案说明
功能要求:实现网页加载后将页面截取成长图片
涉及模块:PyQT5 PIL
逻辑说明:
1:完成窗口设置,利用PyQT5 QWebEngineView加载网页地址,待网页加载完成后,调用check_pag;
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('易哈佛')
self.temp_height = 0
self.setWindowFlag(Qt.WindowMinMaxButtonsHint, False) # 禁用最大化,最小化
# self.setWindowFlag(Qt.WindowStaysOnTopHint, True) # 窗口顶置
self.setWindowFlag(Qt.FramelessWindowHint, True) # 窗口无边框 def urlScreenShot(self, url):
self.browser = QWebEngineView()
self.browser.load(QUrl(url))
geometry = self.chose_screen()
self.setGeometry(geometry)
self.browser.loadFinished.connect(self.check_page)
self.setCentralWidget(self.browser) def get_page_size(self):
size = self.browser.page().contentsSize()
self.set_height = size.height()
self.set_width = size.width()
return size.width(), size.height() def chose_screen(self):
width, height = 750, 1370
desktop = QApplication.desktop()
screen_count = desktop.screenCount()
for i in range(0, screen_count):
rect = desktop.availableGeometry(i)
s_width, s_height = rect.width(), rect.height()
if s_width > width and s_height > height:
return QRect(rect.left(), rect.top(), width, height)
return QRect(0, 0, width, height) if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exit(app.exec_())
2:收集页面高度,并计算分次截屏的次数和余量高度;实例化图片合并工具,设置定时器,超时信号发出后,执行exe_command;
def check_page(self):
p_width, p_height = self.get_page_size()
self.page, self.over_flow_size = divmod(p_height, self.height())
if self.page == 0:
self.page = 1
self.ssm = ScreenShotMerge(self.page, self.over_flow_size)
self.timer = QTimer(self)
self.timer.timeout.connect(self.exe_command)
self.timer.setInterval(400)
self.timer.start()
3:exe_command用来控制截图次数,并在每次截图完成后控制网页向下滑屏幕的高度;所有的页面都已截取时,完成图片合并。
def exe_command(self):
if self.page > 0:
self.screen_shot()
self.run_js() elif self.page < 0:
self.timer.stop()
self.ssm.image_merge()
self.close() elif self.over_flow_size > 0:
self.screen_shot()
self.page -= 1 def run_js(self):
script = """
var scroll = function (dHeight) {
var t = document.documentElement.scrollTop
var h = document.documentElement.scrollHeight
dHeight = dHeight || 0
var current = t + dHeight
if (current > h) {
window.scrollTo(0, document.documentElement.clientHeight)
} else {
window.scrollTo(0, current)
}
}
"""
command = script + '\n scroll({})'.format(self.height())
self.browser.page().runJavaScript(command)
4:screen_shot在每次截图完成后将图片保存,并将图片对象由图片合并根据保存到列表中。
def screen_shot(self):
screen = QApplication.primaryScreen()
winid = self.browser.winId()
pix = screen.grabWindow(int(winid))
name = '{}/temp.png'.format(self.ssm.root_path)
pix.save(name)
self.ssm.add_im(name)
5:截图合并工具,在每次截图完成后将图片对象保存,完成余量截图的重绘和截图的合并。
class ScreenShotMerge():
def __init__(self, page, over_flow_size):
self.im_list = []
self.page = page
self.over_flow_size = over_flow_size
self.get_path() def get_path(self):
self.root_path = Path(__file__).parent.joinpath('temp')
if not self.root_path.exists():
self.root_path.mkdir(parents=True)
self.save_path = self.root_path.joinpath('merge.png') def add_im(self, path):
if len(self.im_list) == self.page:
im = self.reedit_image(path)
else:
im = Image.open(path)
im.save('{}/{}.png'.format(self.root_path, len(self.im_list) + 1))
self.im_list.append(im) def get_new_size(self):
max_width = 0
total_height = 0
# 计算合成后图片的宽度(以最宽的为准)和高度
for img in self.im_list:
width, height = img.size
if width > max_width:
max_width = width
total_height += height
return max_width, total_height def image_merge(self, ):
if len(self.im_list) > 1:
max_width, total_height = self.get_new_size()
# 产生一张空白图
new_img = Image.new('RGB', (max_width - 15, total_height), 255)
x = y = 0
for img in self.im_list:
width, height = img.size
new_img.paste(img, (x, y))
y += height
new_img.save(self.save_path)
print('截图成功:', self.save_path)
else:
obj = self.im_list[0]
width, height = obj.size
left, top, right, bottom = 0, 0, width, height
box = (left, top, right, bottom)
region = obj.crop(box)
new_img = Image.new('RGB', (width, height), 255)
new_img.paste(region, box)
new_img.save(self.save_path)
print('截图成功:', self.save_path) def reedit_image(self, path):
obj = Image.open(path)
width, height = obj.size
left, top, right, bottom = 0, height - self.over_flow_size, width, height
box = (left, top, right, bottom)
region = obj.crop(box)
return region
截图功能完整代码
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Author:Leslie-x
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PIL import Image
from pathlib import Path class ScreenShotMerge():
def __init__(self, page, over_flow_size):
self.im_list = []
self.page = page
self.over_flow_size = over_flow_size
self.get_path() def get_path(self):
self.root_path = Path(__file__).parent.joinpath('temp')
if not self.root_path.exists():
self.root_path.mkdir(parents=True)
self.save_path = self.root_path.joinpath('merge.png') def add_im(self, path):
if len(self.im_list) == self.page:
im = self.reedit_image(path)
else:
im = Image.open(path)
im.save('{}/{}.png'.format(self.root_path, len(self.im_list) + 1))
self.im_list.append(im) def get_new_size(self):
max_width = 0
total_height = 0
# 计算合成后图片的宽度(以最宽的为准)和高度
for img in self.im_list:
width, height = img.size
if width > max_width:
max_width = width
total_height += height
return max_width, total_height def image_merge(self, ):
if len(self.im_list) > 1:
max_width, total_height = self.get_new_size()
# 产生一张空白图
new_img = Image.new('RGB', (max_width - 15, total_height), 255)
x = y = 0
for img in self.im_list:
width, height = img.size
new_img.paste(img, (x, y))
y += height
new_img.save(self.save_path)
print('截图成功:', self.save_path)
else:
obj = self.im_list[0]
width, height = obj.size
left, top, right, bottom = 0, 0, width, height
box = (left, top, right, bottom)
region = obj.crop(box)
new_img = Image.new('RGB', (width, height), 255)
new_img.paste(region, box)
new_img.save(self.save_path)
print('截图成功:', self.save_path) def reedit_image(self, path):
obj = Image.open(path)
width, height = obj.size
left, top, right, bottom = 0, height - self.over_flow_size, width, height
box = (left, top, right, bottom)
region = obj.crop(box)
return region class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('易哈佛')
self.temp_height = 0
self.setWindowFlag(Qt.WindowMinMaxButtonsHint, False) # 禁用最大化,最小化
# self.setWindowFlag(Qt.WindowStaysOnTopHint, True) # 窗口顶置
self.setWindowFlag(Qt.FramelessWindowHint, True) # 窗口无边框 def urlScreenShot(self, url):
self.browser = QWebEngineView()
self.browser.load(QUrl(url))
geometry = self.chose_screen()
self.setGeometry(geometry)
self.browser.loadFinished.connect(self.check_page)
self.setCentralWidget(self.browser) def get_page_size(self):
size = self.browser.page().contentsSize()
self.set_height = size.height()
self.set_width = size.width()
return size.width(), size.height() def chose_screen(self):
width, height = 750, 1370
desktop = QApplication.desktop()
screen_count = desktop.screenCount()
for i in range(0, screen_count):
rect = desktop.availableGeometry(i)
s_width, s_height = rect.width(), rect.height()
if s_width > width and s_height > height:
return QRect(rect.left(), rect.top(), width, height)
return QRect(0, 0, width, height) def check_page(self):
p_width, p_height = self.get_page_size()
self.page, self.over_flow_size = divmod(p_height, self.height())
if self.page == 0:
self.page = 1
self.ssm = ScreenShotMerge(self.page, self.over_flow_size)
self.timer = QTimer(self)
self.timer.timeout.connect(self.exe_command)
self.timer.setInterval(400)
self.timer.start() def exe_command(self):
if self.page > 0:
self.screen_shot()
self.run_js() elif self.page < 0:
self.timer.stop()
self.ssm.image_merge()
self.close() elif self.over_flow_size > 0:
self.screen_shot()
self.page -= 1 def run_js(self):
script = """
var scroll = function (dHeight) {
var t = document.documentElement.scrollTop
var h = document.documentElement.scrollHeight
dHeight = dHeight || 0
var current = t + dHeight
if (current > h) {
window.scrollTo(0, document.documentElement.clientHeight)
} else {
window.scrollTo(0, current)
}
}
"""
command = script + '\n scroll({})'.format(self.height())
self.browser.page().runJavaScript(command) def screen_shot(self):
screen = QApplication.primaryScreen()
winid = self.browser.winId()
pix = screen.grabWindow(int(winid))
name = '{}/temp.png'.format(self.ssm.root_path)
pix.save(name)
self.ssm.add_im(name) if __name__ == '__main__':
url = 'http://blog.sina.com.cn/lm/rank/focusbang//'
app = QApplication(sys.argv)
win = MainWindow()
win.urlScreenShot(url)
win.show()
app.exit(app.exec_())
Python实现网页截图(PyQT5)的更多相关文章
- Python中使用 Selenium 实现网页截图实例
Selenium 是一个可以让浏览器自动化地执行一系列任务的工具,常用于自动化测试.不过,也可以用来给网页截图.目前,它支持 Java.C#.Ruby 以及 Python 四种客户端语言.如果你使用 ...
- Python各种花式截图工具,截到你手软
前言: 最近,项目中遇到了一个关于实现通过给定URL,实现对网页屏幕进行截图的一个功能,前面代码中已经用python的第三方库实现了截图功能,但在上线以后出现了一些bug,所以就改bug的任务就落在了 ...
- Python下载网页的几种方法
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
- 使用PhantomJS实现网页截图服务
这是上半年遇到的一个小需求,想实现网页的抓取,并保存为图片.研究了不少工具,效果都不理想,不是显示太差了(Canvas.Html2Image.Cobra),就是性能不怎么样(如SWT的Brower). ...
- html2canvas 网页截图 下载 上传
利用html2canvas插件 对网页截图 并下载和上传图片. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...
- Python编写网页爬虫爬取oj上的代码信息
OJ升级,代码可能会丢失. 所以要事先备份. 一開始傻傻的复制粘贴, 后来实在不能忍, 得益于大潇的启示和聪神的原始代码, 网页爬虫走起! 已经有段时间没看Python, 这次网页爬虫的原始代码是 p ...
- 爬虫学习笔记(1)-- 利用Python从网页抓取数据
最近想从一个网站上下载资源,懒得一个个的点击下载了,想写一个爬虫把程序全部下载下来,在这里做一个简单的记录 Python的基础语法在这里就不多做叙述了,黑马程序员上有一个基础的视频教学,可以跟着学习一 ...
- iPhone 收藏网址[添加到书签] 和 [添加到主屏幕] 显示自定义图标,而不是网页截图
iPhone 收藏网址[添加到书签] 和 [添加到主屏幕] 显示自定义图标,而不是网页截图: <!-- Safari浏览器[添加到书签] --> <link rel="sh ...
- chrome也可以整张网页截图,保存完整网页为图片
转自:http://www.webkaka.com/blog/archives/chrome-save-a-webpage.html 关于浏览器截图,一直以为Chrome无能为力,最近发现,原来Chr ...
随机推荐
- webpack4-用之初体验,一起敲它十一遍
众所周知,webpack进入第4个大版本已经有2个月的时间了,而且webpack团队升级更新的速度也是非常的惊人 在写下如下内容的时候webpack已经出到了4.6的版本了,剑指5.0应该是指日可待了 ...
- 20135202闫佳歆--week5 课本18章学习笔记
第十八章 调试 内核级开发的调试工作远比用户级开发艰难的多. 一.准备开始 准备工作需要的是: 一个bug 一个藏匿bug的内核版本 相关内核代码的知识和运气 在这一章里,调试的主要思想是让bug重现 ...
- blob下载出现多余乱码内容
blob需要单独获取,,不能通过map来获取 jdbcTemplate.query(sqlcontent, new Object[] {id}, new AbstractLobStreamin ...
- Git(2.14.1版本)学习及使用(一)
OuZeBo原创作品.转载请注明出处 http://www.cnblogs.com/OuZeBo/p/7477465.html 1.下载git:https://git-scm.com/ 2.安装(本人 ...
- (Alpha)Let's-NABC
效果图 NABC Need 在日常生活中我们也许经常有这样的疑问:在空闲时间内经常感到无处可去:抑或是有个心血来潮的想法却无人响应:或者困扰于自己狭小的交际圈,希望能有更多的朋友.(打个羽毛球没人一起 ...
- Beta版本冲刺(一)
目录 组员情况 组员1(组长):胡绪佩 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最新成果 团 ...
- alpha冲刺随笔(一)
项目Alpha冲刺(团队) Alpha冲刺第一天 姓名 学号 博客链接 何守成 031602408 http://www.cnblogs.com/heshoucheng/ 黄锦峰 031602411 ...
- [转帖]MerkleDAG全面解析 一文读懂什么是默克尔有向无环图
MerkleDAG全面解析 一文读懂什么是默克尔有向无环图 2018-08-16 15:58区块链/技术 MerkleDAG作为IPFS的核心数据结构,它融合了Merkle Tree和DAG的优点,今 ...
- PHPSQL注入
什么是SQL注入? 就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 例如一个简单的登录表单(这里把密码写成明文方便说明): 当在表 ...
- shit aliyun
shit aliyun 垃圾的 aliyun 什么鬼逻辑,怎么填写都不对,holy shit! 校验规则: // 对不起,昵称包含不可接受字符或被管理员屏蔽,请选择其它昵称 // 昵称的长度必须大于等 ...