推荐几个优秀的java爬虫项目

java爬虫项目
 
大型的:
适合做搜索引擎,分布式爬虫是其中一个功能。
比较成熟的爬虫。

小型的:
目标是在让你在5分钟之内写好一个爬虫。参考了crawler4j,如果经常需要写爬虫,需要写很多爬虫,还是不错的,因为上手肯定不止5分钟。缺点是它的定制性不强。
垂直、全栈式、模块化爬虫。更加适合抓取特定领域的信息。它包含了下载、调度、持久化、处理页面等模块。每一个模块你都可以自己去实现,也可以选择它已经帮你实现好的方案。这就有了很强的定制性。
看看它的例子:

编写第一个爬虫
 
 1 import us.codecraft.webmagic.Page;
 2 import us.codecraft.webmagic.Site;
 3 import us.codecraft.webmagic.Spider;
 4 import us.codecraft.webmagic.processor.PageProcessor;
 5 
 6 public class GithubRepoPageProcessor implements PageProcessor {
 7 
 8     private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
 9 
10     @Override
11     public void process(Page page) {
12         page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
13         page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
14         page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
15         if (page.getResultItems().get("name")==null){
16             //skip this page
17             page.setSkip(true);
18         }
19         page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
20     }
21 
22     @Override
23     public Site getSite() {
24         return site;
25     }
26 
27     public static void main(String[] args) {
28         Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
29     }
30 }

 

使用注解编写爬虫
 1 @TargetUrl("https://github.com/\\w+/\\w+")
 2 @HelpUrl("https://github.com/\\w+")
 3 public class GithubRepo {
 4 
 5     @ExtractBy(value = "//h1[@class='entry-title public']/strong/a/text()", notNull = true)
 6     private String name;
 7 
 8     @ExtractByUrl("https://github\\.com/(\\w+)/.*")
 9     private String author;
10 
11     @ExtractBy("//div[@id='readme']/tidyText()")
12     private String readme;
13 
14     public static void main(String[] args) {
15         OOSpider.create(Site.me().setSleepTime(1000)
16                 , new ConsolePageModelPipeline(), GithubRepo.class)
17                 .addUrl("https://github.com/code4craft").thread(5).run();
18     }
19 }

 

两种方式,都可以实现对github项目的抓取。
 
原创:偉少
posted @ 2016-05-06 16:08  Chinway.Y  阅读(1736)  评论(0编辑  收藏  举报