網(wǎng)頁(yè)圖片抓取

一個(gè)很簡(jiǎn)單的網(wǎng)站圖片抓取功能

全部使用jdk的基本功能,沒(méi)有用任何包
不要談什么效率,什么規(guī)范,要的就是立即看懂,拿著就改
不好意思,連正則都懶得用
也沒(méi)用開(kāi)源的爬蟲(chóng)軟件。。。。。。

主要分兩部分

1.找出連接

public class GetWebContent {
    /**
     * 獲取html內(nèi)容
     * @param domain
     * @return
     */
    public static String getWebCon(String path) {
        // System.out.println("開(kāi)始讀取內(nèi)容...("+domain+")");
        StringBuffer sb = new StringBuffer();
        try {
            java.net.URL url = new java.net.URL(path);
            BufferedReader in = new BufferedReader(new InputStreamReader(url
                    .openStream()));
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
        } catch (Exception e) { // Report any errors that arise
            sb.append(e.toString());
            System.err.println(e);
            System.err
                    .println("Usage:   java   HttpClient   <URL>   [<filename>]");
        }
        return sb.toString();
    }
     
    /**
     * 下載指定格式鏈接
     * @param path 圖片所在網(wǎng)頁(yè)
     * @param begin 圖片路徑開(kāi)始
     * @param end 圖片路徑結(jié)尾
     */
    public static void uploadImage(String path,String begin,String end){
        Map<String,String> map=new HashMap<>();
        String a=getWebCon(path);
        String[] as=a.split(begin);
        for (int j = 1; j < as.length-1; j++) {
            String xxx = as[j].split(end)[0];
            String url=begin+xxx+end;
            if (map.containsKey(url))  continue;
            try {
                DownloadImage.download(url);
                map.put(url, "111");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
        }
        
    }
}

2.下載圖片

public class DownloadImage {
    
    private  static final String FILE_PATH="D:\\image\\";
 
    public static void download(String urlString) throws Exception {
        // 構(gòu)造URL
        String filename=System.currentTimeMillis()+"-"+(int)(Math.random()*10000)+".jpg";
        String savePath=FILE_PATH;
        URL url = new URL(urlString);
        // 打開(kāi)連接
        URLConnection con = url.openConnection();
        //設(shè)置請(qǐng)求超時(shí)為5s
        con.setConnectTimeout(5*1000);
        // 輸入流
        InputStream is = con.getInputStream();
    
        // 1K的數(shù)據(jù)緩沖
        byte[] bs = new byte[1024];
        // 讀取到的數(shù)據(jù)長(zhǎng)度
        int len;
        // 輸出的文件流
       File sf=new File(savePath);
       if(!sf.exists()){
           sf.mkdirs();
       }
       OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
        // 開(kāi)始讀取
        while ((len = is.read(bs)) != -1) {
          os.write(bs, 0, len);
        }
        // 完畢,關(guān)閉所有鏈接
        os.close();
        is.close();
    } 

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容