Golang爬虫-HeadlessChrome

Headless Chrome安装与使用

Headless Chrome 是 Chrome 浏览器的无界面形态,可以在不打开浏览器的前提下,使用所有 Chrome 支持的特性运行你的程序。相比于现代浏览器,Headless Chrome 更加方便测试 web 应用,获得网站的截图,做爬虫抓取信息等。

安装

对于RobotBug而言Headless Chrome以一个公共资源,可以被其他服务所使用。

docker install chromedp/headless-shell
docker run -d -p 9222:9222 --rm --name headless-shell chromedp/headless-shell
docker exec -it headless-shell bash

语言支持 zh_CN.UTF-8 UTF-8(可选,不影响使用)

chromedp/headless-shell 是基于Debian构建,缺少中文支持,会致使页面乱码

apt-get update
apt-get install locales
dpkg-reconfigure locales

测试

访问:127.0.0.1:9222/json

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9222/devtools/page/73FB7B559C27B630B4A28DF008C2D68E",
   "id": "73FB7B559C27B630B4A28DF008C2D68E",
   "title": "",
   "type": "page",
   "url": "file:///bin/bash",
   "webSocketDebuggerUrl": "ws://127.0.0.1:9222/devtools/page/73FB7B559C27B630B4A28DF008C2D68E"
} ][ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9222/devtools/page/73FB7B559C27B630B4A28DF008C2D68E",
   "id": "73FB7B559C27B630B4A28DF008C2D68E",
   "title": "",
   "type": "page",
   "url": "file:///bin/bash",
   "webSocketDebuggerUrl": "ws://127.0.0.1:9222/devtools/page/73FB7B559C27B630B4A28DF008C2D68E"
} ]

远程访问

headless_shell_test.go

go test -v headless_shell_test.go

项目地址:https://github.com/Ning-Qing/RobotBug
这次绝对不鸽!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go语言中,没有内置的SCP(Secure Copy)功能,但你可以使用第方库来实现SCP功能。一个常用的第三方库是`github.com/pkg/sftp`,它提供了SSH文件传输协议的实现。 要使用该库进行SCP操作,你需要先建立一个SSH连接,然后使用SFTP客户端进行文件传输。下面是一个简单的示例代码: ```go package main import ( "fmt" "golang.org/x/crypto/ssh" "github.com/pkg/sftp" "io" "os" ) func main() { // SSH连接配置 config := &ssh.ClientConfig{ User: "username", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } // 建立SSH连接 conn, err := ssh.Dial("tcp", "example.com:22", config) if err != nil { fmt.Println("Failed to connect to the server:", err) return } defer conn.Close() // 创建SFTP客户端 client, err := sftp.NewClient(conn) if err != nil { fmt.Println("Failed to create SFTP client:", err) return } defer client.Close() // 打开本地文件 localFile, err := os.Open("/path/to/local/file") if err != nil { fmt.Println("Failed to open local file:", err) return } defer localFile.Close() // 创建远程文件 remoteFile, err := client.Create("/path/to/remote/file") if err != nil { fmt.Println("Failed to create remote file:", err) return } defer remoteFile.Close() // 将本地文件内容复制到远程文件 _, err = io.Copy(remoteFile, localFile) if err != nil { fmt.Println("Failed to copy file:", err) return } fmt.Println("File copied successfully!") } ``` 请注意,上述示例代码中的`username`、`password`、`example.com:22`、`/path/to/local/file`和`/path/to/remote/file`需要根据实际情况进行替换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值