1. 安装node环境
  2. node安装puppeteer插件
npm i --save puppeteer --ignore-scripts

3.截图需要js

//导入包
const puppeteer = require('puppeteer');
(async () => {const chromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"const address = "https://news.qq.com/"const path = "/Users/admin/Desktop/test.png"var time = 15000//创建新的浏览器const browser = await puppeteer.launch({ignoreHTTPSErrors: true,headless: false,executablePath: chromePath,args: ['--no-sandbox', '--disable-setuid-sandbox']});//打开新页面const page = await browser.newPage();// 访问await page.goto(address, {waitUntil: 'domcontentloaded'}).catch()await page.waitFor(time)//加载页面的js,cs5var style, textpage.evaluate(function () {var style = document.createElement('style'),text = document.createTextNode('body { background: #fff }')style.setAttribute('type', 'text/css')style.appendChild(text)document.head.insertBefore(style, document.head.firstChild)})let content = await page.content()console.log(content)await page.screenshot({path:path})await browser.close()
})()

4.滚动截屏js

//导入包
const puppeteer = require('puppeteer');
(async () => {//  chrome地址//const chromePath=process.argv[2]const chromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'//传进的将要解析的url网址//const address=process.argv[3];const address = 'https://news.qq.com/'const path = '/Users/admin/Desktop/test.png'var time = 15000const system_warn = 1002 // 系统提示/告警//创建新的浏览器const browser = await puppeteer.launch({ignoreHTTPSErrors: true,headless: false,executablePath: chromePath,args: ['--no-sandbox', '--disable-setuid-sandbox']})//打开新页面const page = await browser.newPage()// 访问await page.goto(address, {waitUntil: 'domcontentloaded'}).catch()await page.setViewport({width: 1920,height: 1080,})await page.waitFor(time)let height_limit = falselet scroll_times = 0let mValues = {'scrollEnable': true, 'height_limit': height_limit, 'times': 2}let result_map = new Map()try {await page.waitFor(5000)while (mValues.scrollEnable) {mValues = await page.evaluate((max_height_px,page_screentshot_height_limit,height_limit,result_map,system_warn,address,scroll_times) => {let times = 1let scrollEnable = trueif (undefined !== document.body && null != document.body) {window.scrollBy(0, window.innerHeight)times = parseInt(document.body.clientHeight / 1080)// 超出图片的限制高度, 生成PDFif (document.body.clientHeight > page_screentshot_height_limit) {height_limit = true}// 超出网页的限制高度, 不再滚动if (document.body.clientHeight > max_height_px && scroll_times > 40) {result_map['resultCode'] = system_warnresult_map['warning'] = '网页加载高度过长, 易造成数据获取失败。'scrollEnable = false}} else {scrollEnable = false}times = times + 1return {'scrollEnable': scrollEnable,'height_limit': height_limit,'times': times,'title': document.title}}, 60000, 60000, height_limit, result_map, system_warn, address, scroll_times)// 等待随机时间let randomMillSecond = randomNum(600, 2000)await sleep(randomMillSecond)scroll_times++console.log(address + ' 需要滚动 : ' + mValues.times + '次 , 滚动第[' + scroll_times + ']次')if (scroll_times > mValues.times) {console.log(address + ' 结束')mValues.scrollEnable = false}}//console.log('Dimensions:', dimensions);//加载页面的js,cs5var style, textpage.evaluate(function () {var style = document.createElement('style'),text = document.createTextNode('body { background: #fff }')style.setAttribute('type', 'text/css')style.appendChild(text)document.head.insertBefore(style, document.head.firstChild)})let content = await page.content()//console.log(content)await page.screenshot({path: path, fullPage: true})} catch (e) {console.log(e)console.log('执行异常')} finally {await browser.close()}
})()// 随机数
function randomNum (minNum, maxNum) {switch (arguments.length) {case 1:return parseInt(Math.random() * minNum + 1, 10)case 2:return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)default:return 0}
}// 延时函数
function sleep (delay) {return new Promise((resolve, reject) => {setTimeout(() => {try {resolve(1)} catch (e) {reject(0)}}, delay)})
}

5.java执行cmd命令截图

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;import java.io.InputStream;public static void main(String[] args) {String url = "https://news.qq.com/";Document document = null;//chrome浏览器地址String chromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";//nodejs地址  + 截图的js的地址(两个需要在同一个目录之下)String nodeJSPath = "/usr/local/opt/node@10/bin/node    /Users/admin/Desktop/printscrnnt.js";String BLANK = "    ";String exec =  nodeJSPath + BLANK + chromePath + BLANK + url;try {//执行脚本命令Process process = Runtime.getRuntime().exec(exec);System.err.println("ecec =======> " + exec);InputStream is = process.getInputStream();document = Jsoup.parse(is, "UTF-8", url);try {process.waitFor();} catch (InterruptedException e) {e.printStackTrace();}process.destroy();process = null;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}document.body();}

java chrome puppeteer 截图相关推荐

  1. 解决windows显示开启HDR后chrome内截图泛白问题

    问题再现: 在windows显示中开启hdr后 在chrome中截图便会出现变色问题.(上图为开启HDR后的显示错误情况,下图为未开启的正常情况) 问题分析: 猜测是因为chrome会默认匹配系统的颜 ...

  2. java web 截图_如何以Java实现网页截图技术

    今天看到某网友关于"如何以Java实现网页截图技术"的咨询帖,由于出现该咨询的地点非常不适合较长回复,故以博文形式回答. 事实上,如果您想以Java实现网页截图,也就是" ...

  3. chrome浏览器截图插件有哪些

    chrome浏览器截图插件工具分享,当我们需要使用电脑或手机截图时,就可以使用电脑上的快捷键"Shift+Win+S"来进行截图操作,当然QQ和微信也自带截图功能对于PC端来说太过 ...

  4. 如何以Java实现网页截图技术

    今天看到某网友关于"如何以Java实现网页截图技术"的咨询帖,由于出现该咨询的地点非常不适合较长回复,故以博文形式回答. 事实上,如果您想以Java实现网页截图,也就是" ...

  5. 谷歌浏览器chrome长截图功能,不用工具轻松截图

    在谷歌浏览器chrome中打开需要截图的网页 等待需要截图的网页打开完毕 然后按F12 打开开发者工具 然后按组合键 Ctrl+Shift+P 调出谷歌浏览器chrome长截图需要的面板 然后输入 f ...

  6. chrome 网页截图插件

    chrome 网页截图插件 下载地址:点击下载 提取码:qqr9

  7. Java phantomjs 网页截图

    简介 根据传入的url对网页进行截图.打开网页和截取图片通过软件phantomjs.exe在后台静默完成.注:该过程不需要打开浏览器 优点: 1.该软件支持多个平台,包括Linux,Windows. ...

  8. canvas java 上传截图_在Vue项目中使用html2canvas生成页面截图并上传

    使用方法 项目中引入 npm install html2canvas html代码 //html代码 js代码 // 引入html2canvas import html2canvas from 'ht ...

  9. Java实现qq截图工具

    导读:当今时代是飞速发展的信息时代,人们在对信息的处理中对图像的处理量与日俱增,这一点在文档人员上显得非常突出. 本软件采用Java语言进行模拟qq截图功能,获取屏幕方式灵活,可以获取指定的屏幕,并且 ...

最新文章

  1. 理科生用创意毁灭世界,爆笑!
  2. 漫谈ERP实施服务的三种境界
  3. 【正一专栏】中国足球不是你想不玩就不玩的
  4. 运算符的计算机制和原则
  5. 深入理解SpringBoot(5)
  6. java控制台输入汉字_给我一个JAVA控制台输入中文的实例
  7. 何恺明Focal Loss改进版!GFocal Loss:良心技术,无cost涨点
  8. Hadoop1重新格式化HDFS
  9. 支付宝第三方在线支付接口详解
  10. 面试题:线上CPU飙高如何排查
  11. tcp/ip协议的三次握手4次断开
  12. ICV:中国智能驾驶领跑全球,2026年L2级汽车销量将占全球44%
  13. 高并发下的Nginx优化
  14. 使用QT对接大华网络摄像头SDK的示例程序(建议收藏)
  15. docker(5、网络2) 1、容器间通信的三种方式 2、 容器如何访问外部 3、 外部如何访问容器
  16. Qt Charts示例
  17. STM32 ST-LINK Utility介绍、下载、安装、使用方法
  18. AD中出现off grid pin问题的解决方法
  19. 一个人也要生活的精彩——土豆烧排骨
  20. 贝叶斯公式和极大似然估计详解

热门文章

  1. linux各种挂载点作用,Linux 挂载点目录及其作用
  2. Linux系统下指定应用安装目录时提示 ./configure报-bash: ./configure: No such file or directory
  3. 关于***JLink Error: Error while identifying Cortex-M core.
  4. trello使用技巧:如何在 trello 删除 board
  5. 提升工作效率-Trello
  6. 回顾基础,Java如何在main方法中调用自身的main方法
  7. Gurobi——GRBEnv
  8. 利用Mediapipe和DGL实现火影结印识别与追踪---特殊手势识别
  9. 【分享】VMProtect使用技巧汇集__最好的虚拟机保护软件之一
  10. 防“微”杜渐 Intel如何破解ARM包围之势?