WebCollector下載整站頁面(JAVA網路爬蟲)

來源:互聯網
上載者:User

標籤:webcollector   爬取整站   網站爬取   java爬蟲   開源爬蟲   

    很多業務需要下載整站頁面(有時為多個網站),將頁面按照網站拓撲結構存放。

    下面給出用JAVA爬蟲WebCollector(2.09版本以上)爬取整站網頁並按照網頁拓撲結構儲存到本地的代碼。

     代碼中的抽取器可以作為一個外掛程式複用。

     WebCollector的jar包可到官網下載:WebCollector官網。進入官網後下載webcollector-版本號碼-bin.zip,解壓得到所需jar包。

     

import cn.edu.hfut.dmic.webcollector.crawler.MultiExtractorCrawler;import cn.edu.hfut.dmic.webcollector.extract.Extractor;import cn.edu.hfut.dmic.webcollector.extract.ExtractorParams;import cn.edu.hfut.dmic.webcollector.model.Page;import cn.edu.hfut.dmic.webcollector.util.FileSystemOutput;import cn.edu.hfut.dmic.webcollector.util.FileUtils;import java.io.File;/** * Created by hu on 2015/6/25. */public class HtmlExtractor extends Extractor{    FileSystemOutput fsOutput;    public HtmlExtractor(Page page, ExtractorParams params) {        super(page, params);        /*每次抽取都會執行個體化一個Extractor對象,為了讓所有Extractor對象共用一個FileSystemOutput對象,          在外部執行個體化一個FileSystemOutput對象fsOutput,以參數形式傳給每個Extractor對象,這裡是擷取          外部傳入的FileSystemOutput對象*/        fsOutput= (FileSystemOutput) params.get("fsOutput");    }    @Override    public boolean shouldExecute() {        //我們希望對所有網頁執行此抽取器        return true;    }    @Override    public void extract() throws Exception {        //本程式不需要進行網頁抽取,所以extract()方法中不需要插入代碼    }    @Override    public void output() throws Exception {        fsOutput.output(page);    }    public static void main(String[] args) throws Exception {        /*如果下載檔案夾存在,先刪除檔案夾*/        File downloadDir=new File("download");        if(downloadDir.exists()){            FileUtils.deleteDir(downloadDir);        }        FileSystemOutput fsOutput=new FileSystemOutput("download");        MultiExtractorCrawler crawler=new MultiExtractorCrawler("crawl",true);        crawler.addSeed("http://36kr.com/");        crawler.addRegex("http://36kr.com/.*");        crawler.addExtractor(".*", HtmlExtractor.class, new ExtractorParams("fsOutput",fsOutput));        crawler.start(100);    }}

程式執行後可到download檔案夾中查看儲存的網頁:


WebCollector下載整站頁面(JAVA網路爬蟲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.