Gecco爬蟲(chóng)框架官網(wǎng)
優(yōu)采云 發(fā)布時(shí)間: 2020-06-09 10:24
Gecco是一款用java語(yǔ)言開(kāi)發(fā)的輕量化的易用的網(wǎng)路爬蟲(chóng)。Gecco整合了jsoup、httpclient、fastjson、spring、htmlunit、redission等優(yōu)秀框架,讓您只須要配置一些jquery風(fēng)格的選擇器能夠很快的寫(xiě)出一個(gè)爬蟲(chóng)。Gecco框架有優(yōu)秀的可擴展性,框架基于開(kāi)閉原則進(jìn)行設計爬蟲(chóng)框架,對更改關(guān)掉、對擴充開(kāi)放。同時(shí)Gecco基于非常開(kāi)放的MIT開(kāi)源協(xié)議,無(wú)論你是使用者還是希望共同建立Gecco的開(kāi)發(fā)者,歡迎pull request。如果你喜歡這款爬蟲(chóng)框架請star 或者 fork!
@Gecco(matchUrl="{user}/{project}", pipelines="consolePipeline")
public class MyGithub implements HtmlBean {
private static final long serialVersionUID = -70687225L;
@RequestParameter("user")
private String user;//url中的{user}值
@RequestParameter("project")
private String project;//url中的{project}值
@Text
@HtmlField(cssPath=".repository-meta-content")
private String title;//抽取頁(yè)面中的title
@Text
@HtmlField(cssPath=".pagehead-actions li:nth-child(2) .social-count")
private int star;//抽取頁(yè)面中的star
@Text
@HtmlField(cssPath=".pagehead-actions li:nth-child(3) .social-count")
private int fork;//抽取頁(yè)面中的fork
@Html
@HtmlField(cssPath=".entry-content")
private String readme;//抽取頁(yè)面中的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 getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getStar() {
return star;
}
public void setStar(int star) {
this.star = star;
}
public int getFork() {
return fork;
}
public void setFork(int fork) {
this.fork = fork;
}
public static void main(String[] args) {
GeccoEngine.create()
//工程的包路徑
.classpath("com.geccocrawler.gecco.demo")
//開(kāi)始抓取的頁(yè)面地址
.start("")
//開(kāi)啟幾個(gè)爬蟲(chóng)線(xiàn)程
.thread(1)
//單個(gè)爬蟲(chóng)每次抓取完一個(gè)請求后的間隔時(shí)間
.interval(2000)
//循環(huán)抓取
.loop(true)
//使用pc端userAgent
.mobile(false)
//開(kāi)始運行
.run();
}
}
DynamicGecco的目的是在不定義SpiderBean的情況下實(shí)現爬取規則的運行時(shí)配置。其實(shí)現原理是采用字節碼編程,動(dòng)態(tài)生成SpiderBean,而且通過(guò)自定義的GeccoClassLoader實(shí)現了抓取規則的熱布署。下面是一個(gè)簡(jiǎn)單Demo,更復雜的Demo可以參考com.geccocrawler.gecco.demo.dynamic下的反例。
下面的代碼實(shí)現了爬取規則的運行時(shí)配置:
DynamicGecco.html()
.gecco("{user}/{project}", "consolePipeline")
.requestField("request").request().build()
.stringField("user").requestParameter("user").build()
.stringField("project").requestParameter().build()
.stringField("title").csspath(".repository-meta-content").text(false).build()
.intField("star").csspath(".pagehead-actions li:nth-child(2) .social-count").text(false).build()
.intField("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();
//開(kāi)始抓取
GeccoEngine.create()
.classpath("com.geccocrawler.gecco.demo")
.start("")
.run();
可以看見(jiàn)爬蟲(chóng)框架,DynamicGecco的方法相比傳統的注解形式代碼量大大降低,而且太酷的一點(diǎn)是DynamicGecco支持運行時(shí)定義和更改規則。
請遵循開(kāi)源協(xié)議MIT



