JAVA crawler tools have a lot of, but Gecco is a very light and convenient tool.
First, the project structure chart.
This is a javase MAVEN project, to add a package dependency, others on four files. Log4j.properties plus three Java classes.
1. Configure Log4j.properties First
Log4j.rootlogger =Error,stdout,d,elog4j.appender.stdout=Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern= [%-5p]%d{yyyy-mm-dd hh:mm:ss,sss} method:%l%n%m%NLOG4J.APPENDER.D=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file=Vincent_player_debug.loglog4j.appender.d.append=trueLog4j.appender.d.threshold=DEBUG log4j.appender.d.layout=Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%NLOG4J.APPENDER.E=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file=Vincent_player_error.loglog4j.appender.e.append=trueLog4j.appender.e.threshold=ERROR log4j.appender.e.layout=Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
2, the next handwritten blog.java, there are comments do not explain
PackageCom.cwj.gecco.pojo;ImportCom.geccocrawler.gecco.annotation.Gecco;ImportCom.geccocrawler.gecco.annotation.HtmlField;Importcom.geccocrawler.gecco.annotation.Request;Importcom.geccocrawler.gecco.request.HttpRequest;ImportCom.geccocrawler.gecco.spider.SpiderBean;/** * @authorCWJ * August 6, 2017 * Blog entity class, run the main function from here to parse * Matchurl: To grab the package's destination address * pipelines: jump to the next pipelines*/@Gecco (Matchurl= "http://www.cnblogs.com/boychen/p/7226831.html", pipelines= "Blogpipelines") Public classBlogImplementsspiderbean{/*** Request to send a GET method to the specified URL*/@RequestPrivateHttpRequest request; /*** Grab all the content under this path*/@HtmlField (Csspath= "Body Div#cnblogs_post_body") PrivateString content; PublicHttpRequest getrequest () {returnrequest; } Public voidsetrequest (HttpRequest request) { This. Request =request; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; } }
3, Blogpipelines.java
PackageCom.cwj.gecco.pipelines;ImportCom.cwj.gecco.pojo.Blog;ImportCom.geccocrawler.gecco.annotation.PipelineName;ImportCom.geccocrawler.gecco.pipeline.Pipeline;/** * @authorCWJ * August 6, 2017 * run out of Blog.java according to @pipelinename here*/@PipelineName (Value= "Blogpipelines") Public classBlogpipelinesImplementsPipeline<blog>{ /*** Handle the crawled content here is printed on the console*/ Public voidProcess (Blog blog) {System.out.println (blog.getcontent ()); }}
4, the last is called in main
PackageCom.cwj.gecco.main;ImportCom.geccocrawler.gecco.GeccoEngine; Public classMain { Public Static voidMain (string[] args) {geccoengine.create ()//package path for the project. Classpath ("Com.cwj.gecco") //page address to start crawling. Start ("http://www.cnblogs.com/boychen/p/7226831.html") //Open several crawler threads. Thread (10) //the interval between the time a single crawler crawls a request. Interval (5) //using PC-side useragent. Mobile (false) //Start Running. Run (); }}
5, crawl to the content, log files are deleted by me have warning
Attach Source Address Https://github.com/BeautifulMeet/Gecco
Java crawls Web content using Gecco crawler