參考: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\n", 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.com\gocolly\colly\colly.go:42:2: cannot find package "google.golang.org/appengine/urlfetch" in any of: D:\Go\src\google.golang.org\appengine\urlfetch (from $GOROOT) C:\Users\Administrator\go\src\google.golang.org\appengine\urlfetch (from $GOPATH) E:\goapp\src\google.golang.org\appengine\urlfetch
參考:https://blog.csdn.net/cjj198561/article/details/80631392
原來是由於google.golang.org/appengine這個包的代碼倉庫變了,指向 https://github.com/golang/appengine
於是,執行 gopm get github.com/golang/appengine 將包下載下來,然后在$gopath\src下建一個名為google.golang.org的目錄,
在下載的appengine目錄上用鼠標復制,粘貼到剛才建立的google.golang.org目錄下。(我是將gopm下載的包全部重新復制到了$gopath\src下)
還有幾個包下不了,錯誤提示為:..\golang.org\x\net\html\charset\charset.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.com\golang\appengine復制到$gopath\src\google.golang.org/appengine
將下載目錄下的github.com\golang\text復制到$gopath\src\google.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/