用WebCollector爬取網站的圖片

來源:互聯網
上載者:User

標籤:webcollector   爬取圖片   java爬蟲   圖片   爬蟲   

用WebCollector爬取整站圖片,只需要遍曆整站頁面,然後將URL為.jpg、gif的頁面(檔案)儲存到本地即可。


例如我們爬取一個美食網站,擷取裡面所有的圖片:

import cn.edu.hfut.dmic.webcollector.crawler.BreadthCrawler;import cn.edu.hfut.dmic.webcollector.model.Page;import cn.edu.hfut.dmic.webcollector.util.FileUtils;import java.io.IOException;import java.util.concurrent.atomic.AtomicInteger;import java.util.regex.Pattern;/** * 用WebCollector爬蟲爬取整站圖片 */public class PicCrawler extends BreadthCrawler{    /*用一個整數,不斷自增,來作為下載的圖片的檔案名稱*/    AtomicInteger id=new AtomicInteger(0);    @Override    public void visit(Page page) {                /*不處理非jpg的網頁/檔案*/        if(!Pattern.matches(".*jpg$",page.getUrl())){            return;        }        /*將圖片內容儲存到檔案,page.getContent()擷取的是檔案的byte數組*/        try {            FileUtils.writeFileWithParent("download/"+id.incrementAndGet()+".jpg",page.getContent());            System.out.println("download:"+page.getUrl());        } catch (IOException e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {        PicCrawler crawler=new PicCrawler();        crawler.addSeed("http://www.meishij.net/");        crawler.addRegex("http://.*meishij.net/.*");        crawler.setThreads(50);        crawler.start(10);    }}

代碼將網站圖片儲存到了工程下面的download檔案夾中:



WebCollector爬蟲官網:https://github.com/CrawlScript/WebCollector

WebCollector文檔:http://www.brieftools.info/document/webcollector/

技術討論群:250108697


用WebCollector爬取網站的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.