WebCollector入门教程(中文版)

来源:互联网 发布:2016网络彩票开售在即 编辑:程序博客网 时间:2024/05/29 15:45
WebCollector爬虫官网:https://github.com/CrawlScript/WebCollector

1.将WebCollector导入工程:

    进入WebCollector主页:https://github.com/CrawlScript/WebCollector

    下载:webcollector-版本号-bin.zip

    将解压后文件夹中的所有jar包添加到工程既可。


2.用WebCollector爬取整个网站:

   爬取新华网整站内容:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Demo {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         BreadthCrawler crawler = new BreadthCrawler();  
  5.         crawler.addSeed("http://www.xinhuanet.com/");    
  6.         crawler.addRegex("http://www.xinhuanet.com/.*");       
  7.         /*网页、图片、文件被存储在download文件夹中*/  
  8.         crawler.setRoot("download");  
  9.         /*进行深度为5的爬取*/  
  10.         crawler.start(5);  
  11.     }  
  12. }  

3.用WebCollector进行精准抽取:

爬取《知乎》并进行问题精准抽取的爬虫(JAVA):

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class ZhihuCrawler extends BreadthCrawler{  
  2.   
  3.     /*visit函数定制访问每个页面时所需进行的操作*/  
  4.     @Override  
  5.     public void visit(Page page) {  
  6.         String question_regex="^http://www.zhihu.com/question/[0-9]+";  
  7.         if(Pattern.matches(question_regex, page.getUrl())){  
  8.             System.out.println("正在抽取"+page.getUrl());  
  9.             /*抽取标题*/  
  10.             String title=page.getDoc().title();  
  11.             System.out.println(title);  
  12.             /*抽取提问内容*/  
  13.             String question=page.getDoc().select("div[id=zh-question-detail]").text();  
  14.             System.out.println(question);  
  15.   
  16.         }  
  17.     }  
  18.   
  19.     /*启动爬虫*/  
  20.     public static void main(String[] args) throws IOException{    
  21.         ZhihuCrawler crawler=new ZhihuCrawler();  
  22.         crawler.addSeed("http://www.zhihu.com/question/21003086");  
  23.         crawler.addRegex("http://www.zhihu.com/.*");  
  24.         crawler.start(5);    
  25.     }  
  26.   
  27.   
  28. }  

4.用WebCollector爬取指定URL列表的网页(不需要递归爬取)。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Demo2 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         /*设置递归爬取时每个页面产生的URL数量,这里不需要递归爬取*/  
  5.         Config.topN=0;  
  6.         BreadthCrawler crawler = new BreadthCrawler();  
  7.         crawler.addSeed("http://www.xinhuanet.com/");     
  8.         crawler.addSeed("http://www.sina.com.cn/");  
  9.         crawler.addRegex(".*");   
  10.         /*网页、图片、文件被存储在download文件夹中*/  
  11.         crawler.setRoot("download");  
  12.         /*进行深度为1的爬取*/  
  13.         crawler.start(1);  
  14.     }  
  15. }  


5.用WebCollector爬取站内以及外站内容:

爬取新华网以及新华网内所有外链的内容,以及外链的外链.......

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Demo3 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         BreadthCrawler crawler = new BreadthCrawler();  
  5.         crawler.addSeed("http://www.xinhuanet.com/");         
  6.         /*网页、图片、文件被存储在download文件夹中*/  
  7.         crawler.setRoot("download");  
  8.         /*指定对爬取URL的限制(URL正则)*/  
  9.         crawler.addRegex(".*");  
  10.         /*进行深度为5的爬取*/  
  11.         crawler.start(5);  
  12.     }  
  13. }  


6.高级参数配置:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Demo4 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         BreadthCrawler crawler = new BreadthCrawler();  
  5.         crawler.addSeed("http://www.xinhuanet.com/");     
  6.           
  7.         /*URL信息存放路径*/  
  8.         crawler.setCrawlPath("crawl");  
  9.           
  10.         /*网页、图片、文件被存储在download文件夹中*/  
  11.         crawler.setRoot("download");  
  12.           
  13.         /*正规则,待爬取网页至少符合一条正规则,才可以爬取*/  
  14.         crawler.addRegex("+^http://www.xinhuanet.com/");          
  15.         crawler.addRegex("+^http://news.xinhuanet.com.*");  
  16.           
  17.         /*负规则,只要符合一条负规则,跳过,不爬取*/  
  18.         crawler.addRegex("-^http://news.xinhuanet.com/edu.*");  
  19.           
  20.         /*线程数*/  
  21.         crawler.setThreads(30);  
  22.           
  23.         /*设置User-Agent*/  
  24.         crawler.setUseragent("Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0");  
  25.           
  26.         /*设置cookie*/  
  27.         crawler.setCookie("你的Cookie");  
  28.           
  29.         /*设置是否支持断点爬取*/  
  30.         crawler.setResumable(false);  
  31.           
  32.         /*进行深度为5的爬取*/  
  33.         crawler.start(5);  
  34.     }  
  35. }  

0 0
原创粉丝点击