Java Crawler Gecco Tool Grab News instance _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.