简单的使用jsoup爬取图片
2015-11-02 17:24
573 查看
简单的使用jsoup爬取图片
首先,先要导入相关的jsoup.jar才能够使用jsoup[jsoup.jar下载地址](http://download.csdn.net/detail/guanglancomet/9234349)
[code] package com.guanglan.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class DownLoadPic { private static final String saveImgPath="E://imgs"; //图片保存路径 public void getDoc() throws IOException{ //以网易为例子 Document doc = Jsoup.connect("http://www.163.com/").get(); //获取后缀为png和jpg的图片的元素集合 Elements pngs = doc.select("img[src~=(?i)\\.(png|jpe?g)]"); //遍历元素 for(Element e : pngs){ String src=e.attr("src");//获取img中的src路径 //获取后缀名 String imageName = src.substring(src.lastIndexOf("/") + 1,src.length()); //连接url URL url = new URL(src); URLConnection uri=url.openConnection(); //获取数据流 InputStream is=uri.getInputStream(); //写入数据流 OutputStream os = new FileOutputStream(new File(saveImgPath, imageName)); byte[] buf = new byte[1024]; int l=0; while((l=is.read(buf))!=-1){ os.write(buf, 0, l); } } } public static void main(String[] args) throws IOException { new DownLoadPic().getDoc(); //调用方法 } }
相关文章推荐
- Ext.encode与Ext.decode的JSON转换
- js数组去除重复方法添加
- JS 实现汉字转拼音
- javascript判断浏览器
- 用nw.js开发markdown编辑器-已完成功能介绍
- 详解Javascript中正则表达式的使用
- js中prototype与Property的用法区别
- jsoup解析HTML
- 一张图解释javascript原型、原型链,对象、原型对象,模拟类,模拟继承。
- JS window.open()
- 常用js之 验证非中文 全中文
- JS 加法函数,用来得到精确的加法结果
- js 获取checkBox值,以及对checkBox进行赋值
- json.dumps错误:'utf8' codec can't decode byte解决方案
- JavaScript学习日记3 条件判断
- js 对象使用
- JS小实践3--[]妙用
- js浮动层的控制
- js 无缝滚屏
- 实用JS收录(持续更新)