美文网首页HACKER
爬取图片链接(让简书更漂亮)

爬取图片链接(让简书更漂亮)

作者: 关guan关guan | 来源:发表于2017-11-03 11:39 被阅读0次

    最近开了简书,准备在简书中找些有feel的图片作为插图,刚好也有目标网站的图片链接,但是由于有300+张,不方便一张张下载,所以写了个小工具,直接上java代码:

    package com.guanstudy;
    
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class HelloWorld {
      public static void main(String[] args) throws UnsupportedEncodingException {
        for(int i=0;i<355;i++){
          downloadPicture("http://www.***.com/images_pub/pub_"+(i+1)+".jpg","/Users/user/Documents/pics/pub_"+(i+1)+".jpg"); 
        }
      }
    
      public static void downloadPicture(String urlString, String path) {
        URL url = null;
        try {
          url = new URL(urlString);
          DataInputStream dataInputStream = new DataInputStream(url.openStream());
          FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
          byte[] buffer = new byte[1024];
          int length;
          while ((length = dataInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, length);
          }
          dataInputStream.close();
          fileOutputStream.close();
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    
    fox

    相关文章

      网友评论

        本文标题:爬取图片链接(让简书更漂亮)

        本文链接:https://www.haomeiwen.com/subject/ppftmxtx.html