chrome headless截图
Headless Chrome
Headless Chrome 是 Chrome 浏览器的无界面形态,可以在不打开浏览器的前提下,使用所有 Chrome 支持的特性运行你的程序。相比于现代浏览器,Headless Chrome 更加方便测试 web 应用,获得网站的截图,做爬虫抓取信息等。相比于出道较早的 PhantomJS,SlimerJS 等,Headless Chrome 则更加贴近浏览器环境。
容器部署
https://github.com/Zenika/alpine-chromedocker search alpine-chromedocker pull zenika/alpine-chromedocker container run -d -p 9222:9222 zenika/alpine-chrome --no-sandbox --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 https://www.chromestatus.com/
示例代码
curl http://127.0.0.1:9222/json/version //获取版本等信息
// Command screenshot is a chromedp example demonstrating how to take a
// screenshot of a specific element and of the entire browser viewport.
package mainimport ("context""encoding/json""fmt""io/ioutil""net""net/http""net/url""sync""time""github.com/chromedp/chromedp""github.com/panjf2000/ants/v2"
)type chromeVersion struct {Browser string `json:"Browser"`ProtocalVersion string `json:"Protocol-Version"`UserAgent string `json:"User-Agent"`V8Version string `json:"V8-Version"`WebKitVersion string `json:"WebKit-Version"`WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
}// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {return chromedp.Tasks{chromedp.Navigate(urlstr),chromedp.FullScreenshot(res, quality),}
}func getChromeVersion(address string) (string, error) {u, _ := url.Parse("http://" + address)ip, err := net.ResolveIPAddr("ip", u.Hostname())if err != nil {return "", err}link := "http://" + ip.String() + ":" + u.Port() + `/json/version`resp, err := http.Get(link)if err != nil {return "", err}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {return "", err}chromeVer := &chromeVersion{}json.Unmarshal(body, chromeVer)return chromeVer.WebSocketDebuggerURL, nil
}func myFunc(i interface{}) {// create contextctx, cancel2 := chromedp.NewContext(allocatorContext)defer cancel2()ctx, cancel3 := context.WithTimeout(ctx, 300*time.Second)defer cancel3()ctx, cancel4 := context.WithDeadline(ctx, time.Now().Add(300*time.Second))defer cancel4()var buf []byteif err := chromedp.Run(ctx, fullScreenshot(`https://brank.as/`, 90, &buf)); err != nil {fmt.Println("idx:", i, "failed run")fmt.Println(err)err := chromedp.Cancel(ctx)if err != nil {fmt.Println("Cancel", err)}return}if err := ioutil.WriteFile(fmt.Sprintf("./tmp/%v_fullScreenshot.png", i), buf, 0o644); err != nil {fmt.Println(err)}err := chromedp.Cancel(ctx)if err != nil {fmt.Println("Cancel", err)}
}var allocatorContext context.Contextfunc main() {ws, err := getChromeVersion("127.0.0.1:9222")if err != nil {fmt.Println(err)return}allocatorContexttmp, cancel1 := chromedp.NewRemoteAllocator(context.Background(), ws)defer cancel1()allocatorContext = allocatorContexttmpvar wg sync.WaitGroupdefer ants.Release()runTimes := 100p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {myFunc(i)wg.Done()})defer p.Release()for i := 0; i < runTimes; i++ {wg.Add(1)_ = p.Invoke(int32(i))}wg.Wait()fmt.Printf("running goroutines: %d\n", p.Running())
}
运行结果
在tmp目录生成png截图文件
chrome headless截图相关推荐
- 反爬虫——使用chrome headless时一些需要注意的细节
以前我们介绍过chrome headless的用法(https://www.cnblogs.com/apocelipes/p/9264673.html). 今天我们要稍微提一下其中一个细节. 反爬和w ...
- python selenium 用法 和 Chrome headless
From: http://cuiqingcai.com/2599.html Selenium教程:https://www.yiibai.com/selenium selenium 官方参考文档:htt ...
- 利用Chrome Headless模式网页转PDF
本文转载于:https://blog.csdn.net/aWDac/article/details/80865754 https://blog.csdn.net/xcl168/articl ...
- chrome headless php,chrome headless+php实现打印pdf服务
前言 之前的项目做过浏览器某个页面打印成pdf的服务,所用的是tcpdf这个包,大概思路就是后台写一套跟页面一样的html模版,打印的时候把数据渲染进去,不好的地方在于所见非所得,你得花大把的时间去做 ...
- python chrome headless_实战Chrome Headless数据抓取(上)
先聊聊数据抓取技术选型 在我看来数据抓取可以分为三种场景: 基本稳定的源站格式或者大量的数据抓取.需要蜘蛛集群调度:使用Java比较方便,可以用WebMagic抓取配合Hadoop调度,如果源站经常改 ...
- Ubuntu 无界面使用selenium chrome + headless
Ubuntu 无界面使用selenium chrome + headless 1. 安装 selenium : sudo pip install selenium 2. 安装 chromdriver: ...
- 技能树升级——Chrome Headless模式 - 全栈客栈 - SegmentFault
技能树升级--Chrome Headless模式 - 全栈客栈 - SegmentFault TNPM
- 解决windows显示开启HDR后chrome内截图泛白问题
问题再现: 在windows显示中开启hdr后 在chrome中截图便会出现变色问题.(上图为开启HDR后的显示错误情况,下图为未开启的正常情况) 问题分析: 猜测是因为chrome会默认匹配系统的颜 ...
- python chrome headless_[技巧] chrome headless 爬虫抓取websoket 数据
目录 源起 分析 实践 总结 源起 周末答应了一个朋友帮他看一下一个网站应该怎么爬,费话不说直接先上网站 https://datacenter.jin10.com/price 数据一直在不停的闪,直觉 ...
最新文章
- 北京协和医院骨科完成中国首例机器人全膝人工关节置换手术
- 59. 螺旋矩阵 II(模拟)
- MySQL光标的使用
- VTK:结构化网格之GetLinearPointId
- Python 之 使用 PIL 库做图像处理
- Twain 学习纪录
- matlab二元方程组,用matlab解一个二元方程组,会的进,得到解再回答
- 深入细枝末节,Python的字体反爬虫到底怎么一回事
- 前端校验rules写法:
- 【codeforces 718 CD】C. Sasha and ArrayD. Andrew and Chemistry
- Winform DataGridView中利用WebClient异步加载显示网络地址的图片
- Visual Studio DSL 入门 1 --- 什么是特定领域开发和DSL
- ASP.NET CORE的Code Fist后Models更改了怎么办?
- spring:注解配置AOP
- MATLAB数值计算函数汇总
- IOS开发学习基于swift语言
- C++ STL map插入效率优化
- c语言两个矩形相交部分坐标,C++判断矩形相交的方法
- 【diannaoxitong】高手分享:最新版Office2013的全面介绍
- linux mysql 僵尸进程_linux shell中清理僵尸进程