Golang爬虫框架colly使用浅析

作者:q56731523 时间:2024-02-06 23:28:06 

Golang 是一门非常适合编写网络爬虫的语言,它有着高效的并发处理能力和丰富的网络编程库。下面是一个简单的 Golang 网络爬虫示例:

package main
import (
   "fmt"
   "net/http"
   "io/ioutil"
   "regexp"
)
func main() {
   resp, err := http.Get("https://www.example.com")
   if err != nil {
       fmt.Println("Error:", err)
       return
   }
   defer resp.Body.Close()
   body, err := ioutil.ReadAll(resp.Body)
   if err != nil {
       fmt.Println("Error:", err)
       return
   }
   re := regexp.MustCompile("<title>(.*)</title>")
   title := re.FindStringSubmatch(string(body))[1]
   fmt.Println("Title:", title)
}

这个爬虫的功能是获取指定网站的标题。代码中使用了 Go 的标准库 net/http 和 regexp 来进行网络请求和正则表达式匹配。当然,这只是一个简单的示例,实际上爬虫需要考虑更多的问题,比如反爬虫、数据存储、并发控制等等。

gocolly是用go实现的网络爬虫框架,我这里用来测试的版本是:colly &ldquo;github.com/gocolly/colly/v2&rdquo;

gocolly的网络爬虫还是很强大,下面我们通过代码来看一下这个功能的使用

package main
import (
 "fmt"
 colly "github.com/gocolly/colly/v2"
 "github.com/gocolly/colly/v2/debug"
)
func main() {
 mUrl := "http://www.ifeng.com/"
 //colly的主体是Collector对象,管理网络通信和负责在作业运行时执行附加的回掉函数
 c := colly.NewCollector(
   // 开启本机debug
   colly.Debugger(&debug.LogDebugger{}),
 )
 //发送请求之前的执行函数
 c.OnRequest(func(r *colly.Request) {
   fmt.Println("这里是发送之前执行的函数")
 })
 //发送请求错误被回调
 c.OnError(func(_ *colly.Response, err error) {
   fmt.Print(err)
 })
 //响应请求之后被回调
 c.OnResponse(func(r *colly.Response) {
   fmt.Println("Response body length:", len(r.Body))
 })
 //response之后会调用该函数,分析页面数据
 c.OnHTML("div#newsList h1 a", func(e *colly.HTMLElement) {
   fmt.Println(e.Text)
 })
 //在OnHTML之后被调用
 c.OnScraped(func(r *colly.Response) {
   fmt.Println("Finished", r.Request.URL)
 })
 //这里是执行访问url
 c.Visit(mUrl)
}

运行结果如下:

这里是发送之前执行的函数

[000001] 1 [     1 - request] map["url":"http://www.ifeng.com/"] (0s)
[000002] 1 [     1 - responseHeaders] map["status":"OK" "url":"http://www.ifeng.com/"] (64.9485ms)
Response body length:250326
Finished http://www.ifeng.com/
[000003] 1 [     1 - response] map["status":"OK" "url":"http://www.ifeng.com/"] (114.9949ms)
[000004] 1 [     1 - html] map["selector":"div#newsList h1 a" "url":"http://www.ifeng.com/"] (118.9926ms)
[000005] 1 [     1 - html] map["selector":"div#newsList h1 a" "url":"http://www.ifeng.com/"] (118.9926ms)
[000006] 1 [     1 - scraped] map["url":"http://www.ifeng.com/"] (118.9926ms)

总结一下:

回调函数的调用顺序如下:

OnRequest在发起请求前被调用

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

OnResponse收到回复后被调用

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

OnScraped在OnHTML之后被调用

来源:https://blog.csdn.net/weixin_44617651/article/details/130575725

标签:Go,colly,爬虫
0
投稿

猜你喜欢

  • 78行Python代码实现现微信撤回消息功能

    2021-12-17 17:41:19
  • Pycharm及python安装详细步骤及PyCharm配置整理(推荐)

    2021-03-15 21:55:51
  • python3列表删除大量重复元素remove()方法的问题详解

    2021-02-02 19:26:03
  • WEB开发中合理选择图片格式

    2011-09-22 20:32:06
  • Vue双向绑定原理及实现方法

    2024-05-10 14:16:55
  • Python实现基于C/S架构的聊天室功能详解

    2022-06-14 11:43:38
  • Oracle 11g数据库安装与卸载的方法图解

    2024-01-22 19:54:15
  • ubuntu 安装pyqt5和卸载pyQt5的方法

    2021-08-25 07:18:41
  • django将数组传递给前台模板的方法

    2023-09-26 09:06:40
  • 使用一条INSERT语句完成多表插入

    2010-03-18 11:08:00
  • JavaScript substr() 字符串截取函数使用详解

    2024-06-05 09:55:13
  • Python matplotlib实现折线图的绘制

    2022-05-03 11:14:58
  • Python如何使用cv2.canny进行图像边缘检测

    2021-03-11 20:38:12
  • 六个实用Pandas数据处理代码

    2023-03-01 05:29:00
  • ASP生成静态网页的方法

    2008-02-18 19:20:00
  • 探究Python中isalnum()方法的使用

    2021-12-05 19:05:31
  • python之模拟鼠标键盘动作具体实现

    2022-12-23 10:51:29
  • Python实现京东抢秒杀功能

    2021-12-06 04:50:17
  • SQL Server中单引号的两种处理技巧

    2008-05-23 13:30:00
  • 地图网站的需求功能与体验

    2009-03-01 11:15:00
  • asp之家 网络编程 m.aspxhome.com