一个简单的图片抓取器
来源:互联网 发布:abb机器人编程手册 编辑:程序博客网 时间:2024/05/18 22:12
今天练习写了一个简单的网络图片抓取器。 麻麻再也不用担心我一张一张右键了。
需要导入jsoup包和io包。
下面来看一下具体的代码实现吧。
Util.java
package com.csu.test;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;public class Util {/** * @author CSU-Max * @param strUrl图片的路径 * @param path保存的路径 * @throws IOException */public void download(String strUrl, String path) throws IOException {URL url = null;InputStream is = null;OutputStream os = null;byte[] buffer = new byte[8192];int bytesRead = 0;try {url = new URL(strUrl);is = url.openStream();os = new FileOutputStream(path);while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{is.close();os.close();}}}
Main.java
package com.csu.test;import java.io.File;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.apache.commons.io.FilenameUtils;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;/** * * @author CSU-Max * 抓取指定网页的图片保存在指定文件夹 * */public class Main {public static void main(String[] args) {//抓取的图片存放的文件夹String filePath = "D:/CSU-Max-temp";//要抓取图片的网页地址String webUrl = "http://xiangce.baidu.com/picture/album/list/85062425ab359f0760819f33a14ec718884ce667";File file = new File(filePath);if(file.exists()){System.out.println("文件夹已经存在");}else{file.mkdir();System.out.println("自动创建文件夹");}try {Document doc = Jsoup.connect(webUrl).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();Elements imgElements = doc.getElementsByTag("img");Set<String> imgSrcSet = new HashSet<String>();for (Element img : imgElements) {String imgSrc = img.attr("abs:src");imgSrcSet.add(imgSrc);}System.out.println("图片总数:"+imgSrcSet.size());Iterator<String> i = imgSrcSet.iterator();while(i.hasNext()){String imgSrc = (String)i.next();String imgName = FilenameUtils.getName(imgSrc);if (imgName.indexOf(".") != -1) {if (imgName.indexOf("?") > -1) {imgName = imgName.substring(0, imgName.indexOf("?"));}Util util = new Util();String saveImagePath = filePath+"/"+imgName;System.out.println("图片抓取开始:");util.download(imgSrc,saveImagePath);System.out.println("图片抓取结束:"+imgSrc+" 保存路径:"+saveImagePath);}} } catch (IOException e) {e.printStackTrace();} }}
哈哈,我下载一下自己百度相册的图片,测试了一下,很好用。
$下载地址:简单的网络图片抓取器 #CSDN@CSU-Max
- 一个简单的图片抓取器
- 简单的图片抓取demo
- shell脚本写的一个图片抓取器
- 简单的抓取网页中的图片
- 简单的python爬虫抓取图片实例
- 简单的python3 urllib3 多线程 抓取图片
- Python 实现简单的网络抓取图片
- 一个PHP的远程图片抓取函数
- 一个简单的网页抓取例子
- WebMagic(一)--抓取一个简单的页面
- WebMagic(一)--抓取一个简单的页面
- 一个简单的图片加载器android
- Python 实现简单的爬虫功能: 图片的抓取
- 用python实现的一个抓取图片的爬虫
- BeautifulSoup的简单使用和抓取静态页面图片
- 分享一个PHP的远程图片抓取函数
- 分享一个PHP的远程图片抓取函数
- 一个PHP的远程图片抓取函数分享
- Struts入门
- effective_java之一:覆写 equals 和 hasCode 方法
- 美国微软总部SDET实习面经
- 使用ant打包APK文件
- Mongodb基础操作学习
- 一个简单的图片抓取器
- Innodb存储引擎查询输出分析(包括查询过程do_select()函数)
- Hashtable and HashMap are two hash based collection in Java and used to store objects as key value p
- Oracle中如何生成36位GUID(带-分隔)
- php (十四) 面向对象 魔术方法 __set()、__get()、__isset() 与 __unset()
- 64位WIN7上成功安装theano,并运行了g++, GPU
- javascript学习(3)——[基础回顾]函数
- android EditText 默认情况下不获取焦点(不弹出输入法)+点击EditText时,不弹出键
- The Most Widely Used Java Libraries