参考:http://www.cnblogs.com/majianguo/p/8186429.html
框架源码在 github.com/gocolly/colly
代码如下(github源码中的demo)
package main import ( "fmt" "github.com/gocolly/colly" ) func main() { // Instantiate default collector c := colly.NewCollector( // Visit only domains: hackerspaces.org, wiki.hackerspaces.org colly.AllowedDomains("hackerspaces.org", "wiki.hackerspaces.org"), ) // On every a element which has href attribute call callback c.OnHTML("a[href]", func(e *colly.HTMLElement) { link := e.Attr("href") // Print link fmt.Printf("Link found: %q -> %s ", e.Text, link) // Visit link found on page // Only those links are visited which are in AllowedDomains c.Visit(e.Request.AbsoluteURL(link)) }) // Before making a request print "Visiting ..." c.OnRequest(func(r *colly.Request) { fmt.Println("Visiting", r.URL.String()) }) // Start scraping on https://hackerspaces.org c.Visit("https://hackerspaces.org/") }
结果Ctrl-B后,提示了类似于cannot find package "github.com/PuerkitoBio/goquery" in any of:等一堆内容,对照提示用gopm逐一下载相应的依赖包,这时候真希望能用go get啊
有一个包下不来,错误提示如下:
..github.comgocollycollycolly.go:42:2: cannot find package "google.golang.org/appengine/urlfetch" in any of: D:Gosrcgoogle.golang.orgappengineurlfetch (from $GOROOT) C:UsersAdministratorgosrcgoogle.golang.orgappengineurlfetch (from $GOPATH) E:goappsrcgoogle.golang.orgappengineurlfetch
参考:https://blog.csdn.net/cjj198561/article/details/80631392
原来是由于google.golang.org/appengine这个包的代码仓库变了,指向 https://github.com/golang/appengine
于是,执行 gopm get github.com/golang/appengine 将包下载下来,然后在$gopathsrc下建一个名为google.golang.org的目录,
在下载的appengine目录上用鼠标复制,粘贴到刚才建立的google.golang.org目录下。(我是将gopm下载的包全部重新复制到了$gopathsrc下)
还有几个包下不了,错误提示为:..golang.orgx ethtmlcharsetcharset.go:20:2: cannot find package "golang.org/x/text/encoding" in any of:
参考这里:https://blog.csdn.net/weixin_42780662/article/details/84312889
以下为引用的内容
==========================
由于众所周知的原因,golang在下载golang.org的包时会出现访问不了的情况。尤其是x包,很多库都依赖于它。由于x包在github上都有镜像,我们可以使用从github.com上先clone下来,再做软链接的方式曲线救国。
mkdir -p $GOPATH/src/github.com/golang/
git clone https://github.com/golang/sys.git $GOPATH/src/github.com/golang/sys
git clone https://github.com/golang/net.git $GOPATH/src/github.com/golang/net
git clone https://github.com/golang/text.git $GOPATH/src/github.com/golang/text
git clone https://github.com/golang/lint.git $GOPATH/src/github.com/golang/lint
git clone https://github.com/golang/tools.git $GOPATH/src/github.com/golang/tools
git clone https://github.com/golang/crypto.git $GOPATH/src/github.com/golang/crypto
ln -s $GOPATH/src/github.com/golang/ $GOPATH/src/golang.org/x
========================
于是按相应地址用gopm get github.com/golang/text下不来。
用迅雷大法,显示只有6.46M,还是下不来。
再次gopm get github.com/golang/text 终于下来了。可恶的闭关锁国。
最后,对照错误提示,
将下载目录下的github.comgolangappengine复制到$gopathsrcgoogle.golang.org/appengine
将下载目录下的github.comgolang ext复制到$gopathsrcgoogle.golang.org/text
全部就绪后 Ctrl-B,成功。
小结一下:
需要类似 golang.org/x/text 的包,可以到 github.com/golang/text 找
需要google.golang.org/appengine 的包,可以到github.com/golang/appengine 找
最后,官网有更多例子:http://go-colly.org/