想用go练练手(我是win7系统,已从https://studygolang.com/dl 下载了go安装包并安装,比较简单,不详述。

但作为边民,没法go get ,又不敢用梯子,幸亏有爱心大牛们的国内镜像。

1、首先感谢gowalker.com,gopm.io 七牛 以及啃、萝卜、萝卜头go语言缔造者,以及sourcegraph.com ,studygolang, csdn, gitee(这里有一篇从码云clone代码的文章:https://www.cnblogs.com/shen-hua/p/6022740.html)

2、因为国内用不了go get ,从gopm网页手工下载包太麻烦。后来发现有gopm工具。

首先从https://gowalker.org/ 搜索 gopm,根据包在github上的路径github.com/gpmgo/gopm,到gopm.io上下载,保存到相应目录下。

用go install安装,可能要根据错误提示修改目录名,及安装依赖包github.com/Unknwon/com

3、我运行go install后,在$gopath\bin下生成gopm.exe(将这个路径加入环境变量,我的gopath路径设为了E:\goapp,将手工下载的包放到了E:\goapp\src\github.com下。修改环境变量后可能需要重启电脑)

4、我是在$gopath\src\github.com目录下运行:

gopm get  github.com/chromedp/chromedp

经过漫长等待,提示如下,

E:\goapp\src\github.com>gopm get github.com/chromedp/chromedp
[GOPM] [36m01- ::[0m [[31mERROR[0m] Skipped invalid package: net/htt
p/httptrace@branch:<UTD>
[GOPM] [36m01- ::[0m [[31mERROR[0m] golang.org/x/image: fail to rena
me directory: rename C:/Users/Administrator/.gopm/repos/golang.org/x/image-183be
bdce1b249c42a7cf6772817e8c2e873b966 C:/Users/Administrator/.gopm/repos/golang.or
g/x/image: Access is denied.

心中一惊,难道又要困死墙内!仔细一看,不是大问题,怀疑是360搞的鬼。于是按照提示手工修改文件名为 C:/Users/Administrator/.gopm/repos/golang.or

g/x/image,成功。(我的是gopm将包下载到了C:\Users\Administrator\.gopm\repos\ 之下。)

6、现在可以到http://www.runoob.com/go/go-tutorial.html入门学习了。

7、使用以下代码测试一下chromedp

// Command screenshot is a chromedp example demonstrating how to take a
// screenshot of a specific element.
package main import (
"context"
"io/ioutil"
"log"
"time" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
) func main() {
var err error // create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel() // create chrome instance
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
} // run task list
var buf []byte
//err = c.Run(ctxt, screenshot(`https://brank.as/`, `#contact-form`, &buf))
err = c.Run(ctxt, screenshot(`http:/baidu.com/`, `#wrapper`, &buf)) if err != nil {
log.Fatal(err)
} // shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
} // wait for chrome to finish
err = c.Wait()
if err != nil {
log.Fatal(err)
} err = ioutil.WriteFile("contact-form.png", buf, )
if err != nil {
log.Fatal(err)
}
} func screenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
//设置UserAgent,不知道成功否?
chromedp.ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
a := map[string]interface{}{
"UserAgent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"}
network.SetExtraHTTPHeaders(a).
Do(ctxt, h)
log.Println(a)
return nil
}),
chromedp.Navigate(urlstr),
chromedp.Sleep( * time.Second),
chromedp.WaitVisible(sel, chromedp.ByID),
chromedp.Sleep( * time.Second),
//chromedp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, chromedp.ByQuery),
chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByID),
}
}

运行后,在当前目录下生成了名为contact-form.png百度快照。

可以用以下代码启动headless chrome ,可在windows任务管理器中看到。

exec.Command("cmd", "/c", "start", "chrome", "--headless", "--remote-debugging-port=9222").Run()

搭建golang学习环境,并用chrome headless获取网页内容的更多相关文章

  1. golang使用chrome headless获取网页内容

    如今动态渲染的页面越来越多,爬虫们或多或少都需要用到headless browser来渲染待爬取的页面. 而最近广泛使用的headless browser解决方案PhantomJS已经宣布不再继续维护 ...

  2. Windows下visual studio code搭建golang开发环境

    Windows下visual studio code搭建golang开发环境 序幕 其实环境搭建没什么难的,但是遇到一些问题,主要是有些网站资源访问不了(如:golang.org),导致一些包无法安装 ...

  3. 用Qemu搭建aarch32学习环境

    作者信息 作者: 彭东林 邮箱: pengdonglin137@163.com QQ: 405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linu ...

  4. 用Qemu搭建x86_64学习环境

    作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linux内核 ...

  5. 用Qemu搭建x86学习环境

    作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linux内核 ...

  6. 云端搭建Linux学习环境 链接https://edu.aliyun.com/article/19 (阿里云ECS服务器 )课堂

    云端搭建Linux学习环境 链接https://edu.aliyun.com/article/19 1. 开通云服务器 2 1.包年包月   按量付费(适合测试数据的时候) 2 2.地域   服务器数 ...

  7. 使用Qemu运行Ubuntu文件系统 —— 搭建SVE学习环境(2)

    开发环境 PC:ubuntu18.04 Qemu:4.1 Kernel:Linux-5.2 概述 由于要学习ARM的SVE技术,但是目前还没有支持SVE指令的板子,所以只能用Qemu来模拟,但是发现Q ...

  8. 基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境

    基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境 前言一.环境准备环境介绍软件下载VMware下安装UbuntuUbuntu下Anaconda的安 ...

  9. ubuntu18.04下搭建深度学习环境anaconda2+ cuda9.0+cudnn7.0.5+tensorflow1.7【原创】【学习笔记】

    PC:ubuntu18.04.i5.七彩虹GTX1060显卡.固态硬盘.机械硬盘 作者:庄泽彬(欢迎转载,请注明作者) 说明:记录在ubuntu18.04环境下搭建深度学习的环境,之前安装了cuda9 ...

随机推荐

  1. mybatis_04 resultType和resultMap区别

    resultType 使用resultType进行结果映射时,查询的列名和映射的pojo属性名完全一致,该列才能映射成功. 如果查询的列名和映射的pojo属性名全部不一致,则不会创建pojo对象: 如 ...

  2. 黑客常用 Linux 入侵常用命令

    大学曾误入歧途算是一个脚本小子.... 系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cp ...

  3. Advanced redirection features

    here are three types of I/O, which each have their own identifier, called a file descriptor: standar ...

  4. Java马士兵高并发编程视频学习笔记(一)

    1.同一个资源,同步和非同步的方法可以同时调用 package com.dingyu; public class Y { public synchronized void m1() { System. ...

  5. js-string.js

    //小结// string.charAt(pos) 返回string中pos所在位置的字符 var name = "Cynthia"; var i = name.charAt(0) ...

  6. jQuery效果之jQuery Color animation 色彩动画扩展

    jQuery 的动画方法(animate)支持各种属性的过渡,但是默认并不支持色彩的过渡,该插件正是来补足这一点! PS: 该插件支持 RGBA 颜色的过渡,但是请注意,IE8以下的版本不支持 RGB ...

  7. Deep Learning - 3 改进神经网络的学习方式

    反向传播算法是大多数神经网络的基础,我们应该多花点时间掌握它. 还有一些技术能够帮助我们改进反向传播算法,从而改进神经网络的学习方式,包括: 选取更好的代价函数 正则化方法 初始化权重的方法 如何选择 ...

  8. Implemented the “Importance Sampling of Reflections from Hair Fibers”

      Just the indirect specular pass by importance sampling. With all layers. Manually traced by 3D Ham ...

  9. (后端)SQL SERVER 字符串按数字排序

    应用于B1-1,B1-2,B10-1,B11-1 sqlserver肯定不能按照字符串进行排序,需要进行处理一番: select CONVERT(varchar, LEFT(code,1)),conv ...

  10. (后端)解决code唯一码(java)简便方法

    public String next() { long appBootTimes = systemVariableService.getAppBootTimes(); return Long.toSt ...