Web前端开发网

fly63.com

首页 资源 工具 文章 教程 栏目
  • 在线搜索
  • 文章标签
  • 文章投稿
  • 赞助一下
  • 关于我们

资源分类

AI智能 酷站推荐 招聘/兼职 框架/库 模块/管理 移动端UI框架 Web-UI框架 Js插件 Jquery插件 CSS相关 IDE环境 在线工具 图形动效 游戏框架 node相关 调试/测试 在线学习 社区/论坛 博客/团队 前端素材 图标/图库 建站资源 设计/灵感 IT资讯
网站收录 / 问题反馈

chromedp

分享
复制链接
新浪微博
QQ 好友

扫一扫分享

GitHub:https://github.com/chromedp/chromedp
描述信息:高效采集数据,网络爬虫工具
GitHub

chromedp一个基于Chrome DevTools协议的Go语言库。该库提供了一种简单、高效、可靠的方式来控制Chrome浏览器进行自动化测试和爬取数据。

它可以模拟用户在浏览器中执行各种操作,如点击、输入文本、截取网页长图、将网页内容转换成pdf文档、下载图片等,从而获取到需要采集的数据。


chromedp 安装

安装 chromedp 可以通过 go get 命令来完成:

go get -u /chromedp/chromedp


基础用法

chromedp的基本用法非常简单,只需要定义一个任务列表,然后将其传递给chromedp.Run函数即可。下面是一个简单的例子。 这个例子的功能如下:

  • chromedp.Navigate: 打开https://pkg.go.dev/time网页
  • chromedp.WaitVisible:等待网页加载完成
  • chromedp.Click:点击#example-After标签。也就是网页中的After函数示例
  • chromedp.Value:将示例代码的值读取到example变量中。
  • 最后输出example变量

如下:

package main

import (
 "context"
 "log"
 "time"
    "github.com/chromedp/chromedp"
)

func main() {
 // create chrome instance
 ctx, cancel := chromedp.NewContext(
  context.Background(),
  // chromedp.WithDebugf(log.Printf),
 )
 defer cancel()

 // create a timeout
 ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
 defer cancel()

 // navigate to a page, wait for an element, click
 var example string
 err := chromedp.Run(ctx,
  chromedp.Navigate(`https://pkg.go.dev/time`),
  // wait for footer element is visible (ie, page is loaded)
  chromedp.WaitVisible(`body > footer`),
  // find and click "Example" link
  chromedp.Click(`#example-After`, chromedp.NodeVisible),
  // retrieve the text of the textarea
  chromedp.Value(`#example-After textarea`, &example),
 )
 if err != nil {
  log.Fatal(err)
 }
 log.Printf("Go's time.After example:\n%s", example)
}


截屏

将网页截取成图片有两个函数:chromedp.Screenshot和chromedp.FullScreenshot。 其中chromedp.Screenshot是按网页中的某个div的元素截取。而chromedp.FullScreenshot是截取整个网页。我们看下下面的例子:

package main

import (
 "context"
 "log"
 "os"

 "github.com/chromedp/chromedp"
)

func main() {
 // create context
 ctx, cancel := chromedp.NewContext(
  context.Background(),
  // chromedp.WithDebugf(log.Printf),
 )
 defer cancel()

 // capture screenshot of an element
 var buf []byte
 if err := chromedp.Run(ctx, elementScreenshot(`https://pkg.go.dev/`, `img.Homepage-logo`, &buf)); err != nil {
  log.Fatal(err)
 }
 if err := os.WriteFile("elementScreenshot.png", buf, 0o644); err != nil {
  log.Fatal(err)
 }

 // capture entire browser viewport, returning png with quality=90
 if err := chromedp.Run(ctx, fullScreenshot(`https://brank.as/`, 90, &buf)); err != nil {
  log.Fatal(err)
 }
 if err := os.WriteFile("fullScreenshot.png", buf, 0o644); err != nil {
  log.Fatal(err)
 }

 log.Printf("wrote elementScreenshot.png and fullScreenshot.png")
}

// elementScreenshot takes a screenshot of a specific element.
func elementScreenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
 return chromedp.Tasks{
  chromedp.Navigate(urlstr),
  chromedp.Screenshot(sel, res, chromedp.NodeVisible),
 }
}

// 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),
 }
}

该示例就是通过elementScreenshot函数中截取了https://pkg.go.dev/中的img.Homepage-logo标签的图片。另外一个就是通过fullScreenshot函数来截取了https://brank.as/网站的长图。


仅供个人学习参考/导航指引使用,具体请以第三方网站说明为准,本站不提供任何专业建议。如果地址失效或描述有误,请联系站长反馈~感谢您的理解与支持!

链接: https://fly63.com/nav/4149

更多»
热门资源
5118站长工具
关键词、长尾词挖掘,AI驱动的SEO内容创作辅助平台
官网
fly63工具箱
简单、易用、便捷的在线工具
官网
GitHub
remove.bg
消除图片背景:100% 自动 – 只需 5 秒
官网
retoucher
在线AI智能抠图去背景工具
官网
图片压缩工具
免费减小图片大小,支持JPG/PNG/Webp多种格式
官网
图片格式转换
在线图像转换器,支持JPG/PNG/WebP多种格式
官网
老照片修复神器
AI还原褪色和损坏的照片,在线一键修复老照片
官网
AI图片处理
一站式AI工具平台,为工作和学习提供智能解决方案
官网
证件照生成器
在线制作标准证件照、换底色、AI智能生成
官网
云雾 API
致力于为开发者提供快速、便捷的 Web API 接口调用方案
官网
阿里云DataV
地图数据
官网
Apifox
API 文档、API 调试、API Mock、API 自动化测试一体化协作平台
官网
类似于chromedp的资源
Tarnish
一款针对Chrome扩展的静态安全分析平台
官网
GitHub
Mergely
在线对比文本高亮工具
官网
GitHub
clipber(快贴)
真正的云剪贴板工具
官网
Habitica
游戏化Todo
官网
Witeboard
免费在线白板共享协作工具
官网
ps在线工具【uupoop】
在线PS图片处理工具_ps精简版
官网
鹿班系统
快速生成海量图片,小白也能做设计
官网
Tika
非常实用的文档处理工具包
官网
GitHub
目录

手机扫一扫预览

首页 技术导航 在线工具 技术文章 教程资源 前端标签 AI工具集 前端库/框架

Copyright © 2018 Web前端开发网 All Rights Reserved. 分享编程学习资源(教程/框架/库)、在线工具、技术教程、内容以学习参考为主,助您解决各类实际问题,快速提升专业能力。