likes
comments
collection
share

「Go开源包」Geziyor:一个高性能的网络爬虫框架

作者站长头像
站长
· 阅读数 10

大家好,我是渔夫子。

今天给大家推荐一个高性能的网络爬虫框架:Geziyor。该框架可以用来抓取网站内容并从中提取出结构化的数据。其用途极为广泛,可以用于数据挖掘、监控以及自动化测试。 项目地址:github.com/geziyor/gez…

接下来我们来看Geziyor框架的使用和特点。

基本使用

该框架使用很简单,就是配置对应的选项启动即可。以抓取网址quotes.toscrape.com/ 的内容为例。

指定要抓取的网址

package main

import (
	"github.com/geziyor/geziyor"
)

func main() {
	geziyor.NewGeziyor(&geziyor.Options{
		StartURLs: []string{"http://quotes.toscrape.com/"},
	}).Start()
}

运行上面的代码,就可以抓取StartURLs变量中网址的内容了。那么,如何处理抓取后的结果呢?

指定抓取结果解析函数

通过 ParseFunc选项指定解析结果的函数。我们指定解析函数为quotesParse,如下:

package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/geziyor/geziyor"
	"github.com/geziyor/geziyor/client"
)


func main() {
	geziyor.NewGeziyor(&geziyor.Options{
		StartURLs: []string{"http://quotes.toscrape.com/"},
		ParseFunc: quotesParse,
	}).Start()
}

func quotesParse(g *geziyor.Geziyor, r *client.Response) {
	r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
		g.Exports <- map[string]interface{}{
			"text":   s.Find("span.text").Text(),
			"author": s.Find("small.author").Text(),
		}
		fmt.Printf("debug-s:%+v\n", s)
	})
	if href, ok := r.HTMLDoc.Find("li.next > a").Attr("href"); ok {
		g.Get(r.JoinURL(href), quotesParse)
	}
}

因为ParseFunc的类型是func(g *Geziyor, r *client.Response),所以在quotesParse函数中的入参也是*Geziyor*client.Response

在结果解析函数中,我们还可以做入库操作,来永久保存。当然,还可以将结果导出成json或csv等。

指定结果导出函数

geziyor.Options选项中,通过Exporters参数可执行要导出的格式对象,就可以将解析的结果导出成对应的格式。如下是导出json格式:

package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/geziyor/geziyor"
	"github.com/geziyor/geziyor/client"
	"github.com/geziyor/geziyor/export"
)


func main() {
	geziyor.NewGeziyor(&geziyor.Options{
		StartURLs: []string{"http://quotes.toscrape.com/"},
		ParseFunc: quotesParse,
		Exporters: []export.Exporter{&export.JSON{}},
	}).Start()
}

func quotesParse(g *geziyor.Geziyor, r *client.Response) {
	r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
		g.Exports <- map[string]interface{}{
			"text":   s.Find("span.text").Text(),
			"author": s.Find("small.author").Text(),
		}
		fmt.Printf("debug-s:%+v\n", s)
	})
	if href, ok := r.HTMLDoc.Find("li.next > a").Attr("href"); ok {
		g.Get(r.JoinURL(href), quotesParse)
	}
}

运行该段代码后,可以看到在当前目录下会多了一个out.json的文件,这个文件就是输出的json格式的内容。当然,可以通过export.JSON对象中的FileName属性指定要输出的文件。

我们看下Exporters的类型是一个export.Exporter的切片,代表可以将一个结果同时输出多种格式。同时,export.Exporter是一个接口类型,也就是说只要实现了该export.Exporter的接口,就可以扩展导出的格式。如下:

type Exporter interface {
	Export(exports chan interface{}) error
}

指定请求的并发量

通过选项参数中的ConcurrentRequests: 就可以指定并发量。这里的并发是指的当我们指定了多个要抓取的地址时,可以启动多个协程来做抓取任务。如下:

func main() {
	geziyor.NewGeziyor(&geziyor.Options{
		ConcurrentRequests: 10,
		StartURLs: []string{"http://quotes.toscrape.com/"},
		ParseFunc: quotesParse,
		Exporters: []export.Exporter{&export.JSON{}},
	}).Start()
}

更多选项配置,可以查看geziyor.Options中的配置。就不一一介绍了。

总结

通过该框架,还可以学习如何控制并发数,如何通过中间件的方式来提高框架的扩展性,所以还是值得学习的。

特别说明:你的关注,是我写下去的最大动力。点击下方公众号卡片,直接关注。关注送《100个go常见的错误》pdf文档、经典go学习资料。