Recently saw Gecoo crawler tools, feel more simple and easy to use, all write a demo test, crawl site
Http://zj.zjol.com.cn/home.html, the main grabbing news headlines and publishing time as a grab test object. Crawl HTML nodes through the same as the jquery selector node, very convenient, Gecco code mainly using annotation implementation to achieve URL matching, looks more concise and beautiful.
Add Maven Dependencies
<dependency>
<groupId>com.geccocrawler</groupId>
<artifactid>gecco</ artifactid>
<version>1.0.8</version>
</dependency>
To write a crawl list page
@Gecco (Matchurl = "http://zj.zjol.com.cn/home.html?pageindex={pageindex}& Pagesize={pagesize} ", pipelines =" Zjnewslistpipelines ") public class Zjnewsgeccolist implements Htmlbean {@Request PR
Ivate HttpRequest request;
@RequestParameter private int pageIndex;
@RequestParameter private int pageSize; @HtmlField (Csspath = "#content > div > div > Div.con_index > Div.r.main_mod > div > ul > li > DL & Gt
DT > a ") Private list
@PipelineName ("Zjnewslistpipelines") Public
class Zjnewslistpipelines implements Pipeline<zjnewsgeccolist > {public
void process (Zjnewsgeccolist zjnewsgeccolist) {
HttpRequest request=zjnewsgeccolist.getrequest ();
For (Hrefbean bean:zjNewsGeccoList.getNewList ()) {
//Enter the page crawl
schedulercontext.into (Request.subrequest (" http://zj.zjol.com.cn "+bean.geturl ()));
}
int Page=zjnewsgeccolist.getpageindex () +1;
String Nexturl = "http://zj.zjol.com.cn/home.html?pageIndex=" +page+ "&pagesize=100";
Crawl Next page
Schedulercontext.into (request.subrequest (Nexturl));
}
To write a crawling page of love
@Gecco (Matchurl = "http://zj.zjol.com.cn/news/[code].html", pipelines = "zjnewsdetailpipeline") Public
class Zjnewsdetail implements Htmlbean {
@Text
@HtmlField (csspath = "#headline")
private String title;
@Text
@HtmlField (csspath = "#content > div > Div.news_con > Div.news-content > Div:nth-child (1) > div & Gt P.go-left.post-time.c-gray ")
private String createtime;
}
@PipelineName ("Zjnewsdetailpipeline") Public
class Zjnewsdetailpipeline implements Pipeline<zjnewsdetail > {public
void process (Zjnewsdetail zjnewsdetail) {
System.out.println (zjnewsdetail.gettitle () + "" + Zjnewsdetail.getcreatetime ());
}
Start the main function
public class Main {public
static void Main (String [] rags) {
geccoengine.create ()
/project Package path
. Classpath ( "Com.zhaochao.gecco.zj")
//Start crawl page address
. Start ("http://zj.zjol.com.cn/home.html?pageIndex=1&pageSize=
Mobile (FALSE)
//start running
. Run ();
}
Crawl Results
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.