java 开发图片批量采集系统 java 批量抓取网页图片

通过html解析实现图片批量下载

 

1.   java代码

package com.hanwei.cn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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;

/**
 * 
 * 作者:json
 * 创建日期:2015年12月18日
 * 版本 V1.0	
 * 作用 通过html解析实现图片批量下载
 */
public class HtmJsoup {
	/**
	 * 根据网址和编码获取html页面源代码
	 * @param url 网址
	 * @param encoding 编码
	 * @return 网页源代码
	 */
	public static String getHtmlResouceByUrl(String url,String encoding){
		StringBuffer sb = new StringBuffer();
		URL urlObj = null;
		URLConnection con = null;
		InputStreamReader is= null;
		BufferedReader bir = null;
		try {
			//建立网络连接
			urlObj = new URL(url);
			//打开网络连接
			con =(URLConnection)urlObj.openConnection();
			//创建输入流
			is = new InputStreamReader(con.getInputStream(),encoding);
			//创建一个缓冲流写入文件流
			bir = new BufferedReader(is);
			String line="";
			while((line=bir.readLine())!=null){
				//一行一行的读取追加
				sb.append(line+"\r\n");
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				bir.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	/**
	 * 图片下载
	 * @param imgUrl 图片地址
	 * @param directory 要保存图片的路径
	 */
	public static void downImages(String directory ,String imgPath){
		String fileName = imgPath.substring(imgPath.lastIndexOf("/"));
		//创建文件的目录结构
		File file = new File(directory);
		//不存在就创建
		if(!file.exists()){
			file.mkdirs();
		}
		URL url = null;
		HttpURLConnection con = null;
		FileOutputStream out = null;
		InputStream is = null;
		File fil  = null;
		try {
			//建立网络连接
			url = new URL(imgPath);
			//打开网络连接
			con = (HttpURLConnection)url.openConnection();
			//从内存中读取数据
			is = con.getInputStream();
			//在本地硬盘上创建文件和目录
			fil = new File(directory+fileName);
			//输出到硬盘上,一边读一边写
			out = new FileOutputStream(fil);
			int i=0;
			while((i=is.read())!=-1){
				out.write(i);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		String htmlSource = HtmJsoup.getHtmlResouceByUrl("http://tech.qq.com/", "gb2312");
		//解析网页的源代码
		Document document = Jsoup.parse(htmlSource);
		//获取所有图片的地址
		Elements elements  = document.getElementsByTag("img");
		//遍历获取元素
		for(Element el:elements){
				String imgUrl  = el.attr("src");
				if(!"".equals(imgUrl) && imgUrl.startsWith("http://")){
					System.out.println("正在下载的图片地址:"+imgUrl);
					downImages("D:\\image",imgUrl);
				}
		}
		System.out.println("下载完毕!!!");
	}
}

2.   jar包 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hanchufeng2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值