Golang网络爬虫框架gocolly/colly怎么用

发布时间:2021-12-15 09:26:46 作者:小新
来源:亿速云 阅读:325

小编给大家分享一下Golang网络爬虫框架gocolly/colly怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

安装

go get -u github.com/gocolly/colly/...

例子

import (    "fmt"    "github.com/gocolly/colly")func main() {    c := colly.NewCollector()    c.OnResponse(func(r *colly.Response) {        fmt.Println("IP:", string(r.Body))    })    //c.SetProxy("http://127.0.0.1:1080")    c.Visit("http://ip.cip.cc/")}

SetProxy函数可以用来配置HTTP代理。

colly的主体是Collector对象,管理网络通信和负责在作业运行时执行附加的回掉函数。使用colly需要先初始化Collector:

c := colly.NewCollector()

vgo

vgo是Go语言推出的第三方库管理工具,在Go语言新版本中使用。

常用的命令行:

· go help mod 查看帮助。

· go mod init <项目模块名称>初始化模块,会在项目根目录下生成 go.mod 文件,是可以自己手动编辑的。

依赖包大多在Github上,安装依赖可能会出现连接超时等问题,可以配置全局git代理:

git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https://127.0.0.1:1080#取消代理:git config --global --unset http.proxygit config --global --unset https.proxy

cmd走shadowsocks代理:

set http_proxy=127.0.0.1:1080set https_proxy=127.0.0.1:1080curl cip.ccIP    : 140.206.97.42地址    : 中国  上海数据二    : 上海市 | 联通URL    : http://www.cip.cc/140.206.97.42

Linux使用export设置环境变量,代码同上。

回掉函数的调用顺序

1. OnRequest 在发起请求前被调用。

2. OnError 请求过程中如果发生错误被调用。

3. OnResponse 收到回复后被调用。

4. OnHTML 在OnResponse之后被调用,如果收到的内容是HTML 。

5. OnScraped 在OnHTML之后被调用。

官方提供的Basic示例代码:

package mainimport (    "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/")}

该实例程序仅访问hackerspaces.org域内的链接,OnHTML回掉函数的选择器为a[href],选择页面内具有href属性的a类型元素,找到链接后继续抓取。 运行的部分结果如下:

Visiting https://hackerspaces.org/Link found: "navigation" -> #column-oneLink found: "search" -> #searchInputLink found: "" -> /File:Cbase07.jpgVisiting https://hackerspaces.org/File:Cbase07.jpgLink found: "navigation" -> #column-oneLink found: "search" -> #searchInputLink found: "File" -> #fileLink found: "File history" -> #filehistoryLink found: "File usage" -> #filelinksLink found: "" -> /images/e/ec/Cbase07.jpgVisiting https://hackerspaces.org/images/e/ec/Cbase07.jpgLink found: "800 × 600 pixels" -> /images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpgVisiting https://hackerspaces.org/images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpg

以上是“Golang网络爬虫框架gocolly/colly怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 扒一扒Rancher社区中的小工具
  2. 将intelij idea或者golang 的隐藏文件由C盘移动到其他盘?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang colly

上一篇:WCF绑定原理是什么

下一篇:WCF基础知识有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》