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

go gocolly模块学习

时间:2019-11-13 00:53:00      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:ret   ext   请求   request   res   new   turn   package   collect   

package main

import(
    "fmt"
    "net/http"
    "github.com/gocolly/colly"
)

// test
func testColly(){
    // 创建collector
    c := colly.NewCollector(
        // 填写配置的地方
        // colly.AllowedDomains("hackerspaces.org","wiki.hackspaces.org"),
        colly.MaxDepth(1),
    )

    // 按照html的属性进行爬取相对应内容
    c.OnHTML("a[href]",func(e *colly.HTMLElement){
        link := e.Attr("href") // html的属性
        fmt.Printf("link found:%v->%s\n",e.Text,link)
    })

    // 打印所有的数据
    c.OnHTML("*",func(e *colly.HTMLElement){
        fmt.Println("打印所有的数据",e)
    })

    // 现运行打印这部分
    c.OnRequest(func(r *colly.Request){
        fmt.Println("Visiting...",r.URL.String())
    })
    
    // 想要爬取的网站地址
    c.Visit("https://baidu.com/")
}

func getParameter(w http.ResponseWriter,r *http.Request){
    parameter := r.URL.Query().Get("url")  //获取带有参数的给请求的url
    if parameter==""{
        fmt.Println("get parameter is nill")
        return
    }
    fmt.Println("参数url的值:",parameter)
    fmt.Fprintln(w,parameter)
}

func main(){
    fmt.Println("学习gocolly库")
    testColly()
}

爬虫有危险,须谨慎

go gocolly模块学习

标签:ret   ext   请求   request   res   new   turn   package   collect   

原文地址:https://www.cnblogs.com/MyUniverse/p/11846080.html

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