Gecco:易用的轻量化网络爬虫介绍

Gecco:易用的轻量化网络爬虫介绍

技术背景

在网络数据获取的需求下,爬虫技术应运而生。Gecco 是一款使用 Java 语言开发的易用的轻量化网络爬虫。它集成了 jsoup、httpclient、fastjson、spring、htmlunit、redission 等优秀框架,基于开闭设计原则,具有良好的扩展性,并且遵循非常开放的 MIT 开源协议。

实现步骤

1. 下载 Gecco

可以通过 Maven 进行下载,在 pom.xml 中添加以下依赖:

1
2
3
4
5
<dependency>
<groupId>com.geccocrawler</groupId>
<artifactId>gecco</artifactId>
<version>x.x.x</version>
</dependency>

2. 快速开始

定义一个爬虫类,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import com.geccocrawler.gecco.annotation.Gecco;
import com.geccocrawler.gecco.annotation.Html;
import com.geccocrawler.gecco.annotation.HtmlField;
import com.geccocrawler.gecco.annotation.RequestParameter;
import com.geccocrawler.gecco.annotation.Text;
import com.geccocrawler.gecco.spider.HtmlBean;

@Gecco(matchUrl="https://github.com/{user}/{project}", pipelines="consolePipeline")
public class MyGithub implements HtmlBean {

private static final long serialVersionUID = -7127412585200687225L;

@RequestParameter("user")
private String user;

@RequestParameter("project")
private String project;

@Text
@HtmlField(cssPath=".pagehead-actions li:nth-child(2) .social-count")
private String star;

@Text
@HtmlField(cssPath=".pagehead-actions li:nth-child(3) .social-count")
private String fork;

@Html
@HtmlField(cssPath=".entry-content")
private String readme;

public String getReadme() {
return readme;
}

public void setReadme(String readme) {
this.readme = readme;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getProject() {
return project;
}

public void setProject(String project) {
this.project = project;
}

public String getStar() {
return star;
}

public void setStar(String star) {
this.star = star;
}

public String getFork() {
return fork;
}

public void setFork(String fork) {
this.fork = fork;
}

public static void main(String[] args) {
GeccoEngine.create()
.classpath("com.geccocrawler.gecco.demo")
.start("https://github.com/xtuhcy/gecco")
.thread(1)
.interval(2000)
.loop(true)
.mobile(false)
.start();
}
}

3. 使用 DynamicGecco 进行运行时配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.dynamic.DynamicGecco;

public class DynamicGeccoDemo {
public static void main(String[] args) {
DynamicGecco.html()
.gecco("https://github.com/{user}/{project}", "consolePipeline")
.requestField("request").request().build()
.stringField("user").requestParameter("user").build()
.stringField("project").requestParameter().build()
.stringField("star").csspath(".pagehead-actions li:nth-child(2) .social-count").text(false).build()
.stringField("fork").csspath(".pagehead-actions li:nth-child(3) .social-count").text().build()
.stringField("contributors").csspath("ul.numbers-summary > li:nth-child(4) > a").href().build()
.register();

GeccoEngine.create()
.classpath("com.geccocrawler.gecco.demo")
.start("https://github.com/xtuhcy/gecco")
.run();
}
}

核心代码解释

快速开始代码

  • @Gecco 注解:用于定义爬虫的匹配 URL 和管道。
  • @RequestParameter 注解:用于获取请求参数。
  • @Text@Html 注解:用于提取文本和 HTML 内容。
  • @HtmlField 注解:通过 jQuery 风格的选择器提取元素。

DynamicGecco 代码

通过 DynamicGecco.html() 开始配置,然后使用一系列的 stringField 等方法定义字段和提取规则,最后通过 register() 注册规则。

最佳实践

  • 可以使用 Redis 实现分布式爬虫,参考 gecco-redis
  • 结合 Spring 开发业务逻辑,参考 gecco-spring
  • 支持 htmlunit 扩展,参考 gecco-htmlunit

常见问题

  • 依赖问题:确保添加了所有必要的依赖,如 httpclientjsoupfastjson 等。
  • 规则配置错误:检查 cssPath 等选择器是否正确,确保能够准确提取元素。

Gecco:易用的轻量化网络爬虫介绍
https://119291.xyz/posts/gecco-lightweight-web-crawler-introduction/
作者
ww
发布于
2025年5月21日
许可协议