网页图片抓取

一个很简单的网站图片抓取功能

全部使用jdk的基本功能,没有用任何包
不要谈什么效率,什么规范,要的就是立即看懂,拿着就改
不好意思,连正则都懒得用
也没用开源的爬虫软件。。。。。。

主要分两部分

1.找出连接

public class GetWebContent {
    /**
     * 获取html内容
     * @param domain
     * @return
     */
    public static String getWebCon(String path) {
        // System.out.println("开始读取内容...("+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 图片所在网页
     * @param begin 图片路径开始
     * @param end 图片路径结尾
     */
    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 {
        // 构造URL
        String filename=System.currentTimeMillis()+"-"+(int)(Math.random()*10000)+".jpg";
        String savePath=FILE_PATH;
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        //设置请求超时为5s
        con.setConnectTimeout(5*1000);
        // 输入流
        InputStream is = con.getInputStream();
    
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
       File sf=new File(savePath);
       if(!sf.exists()){
           sf.mkdirs();
       }
       OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
          os.write(bs, 0, len);
        }
        // 完毕,关闭所有链接
        os.close();
        is.close();
    } 

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,192评论 25 708
  • 33款可用来抓数据的开源爬虫软件工具 要玩大数据,没有数据怎么玩?这里推荐一些33款开源爬虫软件给大家。 爬虫,即...
    visiontry阅读 7,750评论 1 99
  • 用真诚,爱心去经营自己的账户,多存少取,真正的幸福也是给予和付出。
    梅子Yoga阅读 236评论 0 1
  • 碧海蓝天,海鸥集翔。 迟暮之年,面朝大海。 年少不知梦几何,唯有时光匆匆去。 皱纹满面头发白,再去追梦力却无。
    三w点阅读 385评论 1 9

友情链接更多精彩内容