zoukankan      html  css  js  c++  java
  • go gocolly模块学习

    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
    ",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()
    }
    

    爬虫有危险,须谨慎

  • 相关阅读:
    lnmp分离及其迁移数之一---数据库迁移
    lnmp wordpress...
    LNMP安装
    rpm 强制卸载
    ss ifconfig工具
    nginx--日志
    nginx--模块2--基于用户
    python-网络编程
    基本数据之-字典
    Python【day 9】函数入门1
  • 原文地址:https://www.cnblogs.com/MyUniverse/p/11846080.html
Copyright © 2011-2022 走看看