码迷,mamicode.com
首页 > 其他好文 > 详细

试验一下Golang 网络爬虫框架gocolly/colly

时间:2019-02-02 17:41:05      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:port   代码   pack   com   源码   ...   art   string   ctrl   

参考:http://www.cnblogs.com/majianguo/p/8186429.html

框架源码在 github.com/gocolly/colly

代码如下(源码中的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啊

 

试验一下Golang 网络爬虫框架gocolly/colly

标签:port   代码   pack   com   源码   ...   art   string   ctrl   

原文地址:https://www.cnblogs.com/pu369/p/10348572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!