java 网页图片抓取

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;

public class DataDownUtil {

	/**
	 * 根据图片的url地址,下载图片到服务器
	 * @param filePath	图片保存的路径  
	 * @param imageUrl		图片的原始地址
	 * @return void 无返回值
	 */
	public static void downImage(String filePath , String imageUrl ){
		
		String fileName = imageUrl.substring(imageUrl.lastIndexOf("/"));
		File files = null ;
		URL url = null;
		HttpURLConnection connection = null;
		File file = null;
		InputStream is = null;
		FileOutputStream fos = null;
		
		try {
			//创建文件目录
			files = new File(filePath);
			if(!files.exists()){
				files.mkdirs();
			}
			//获取网络秃瓢的下载地址
			 url = new URL(imageUrl);
			//获得网络连接
			 connection = (HttpURLConnection) url.openConnection();
			//获得连接的输出流
			 is = connection.getInputStream();
			//创建文件
			 file = new File(filePath+fileName);
			//创建输入流
			 fos = new FileOutputStream(file);
			int temp = 0;
			while((temp = is.read() ) != -1){
				fos.write(temp);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {
				if( is !=  null){
					is.close();
				}
				if( fos !=  null){
					fos.close();
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	
	/**
	 * 根据url网址和页面编码 获取网页的源代码
	 * @param url  需要下载源代码的网址
	 * @param encoding  页面的编码集 
	 * @return String  网页源代码
	 */
	public static String getHtmlResourceByUrl(String url,String encoding){
			
			//存储源代码
			StringBuffer buffer = new  StringBuffer();
			URL urlObject = null;
			URLConnection uc = null;
			InputStreamReader isr = null;
			BufferedReader br = null;
			
			try {
				//建立网络连接
				urlObject = new URL(url);
				//打开网络连接
				 uc = urlObject.openConnection();
				//建立文件的写入流
				 isr = new InputStreamReader(uc.getInputStream(),encoding);
				//建立缓存写入流
				 br = new BufferedReader(isr);
				//临时变量
				String temp = null ;
				while( ( temp=br.readLine ( ) ) != null){
					buffer.append(temp + "\n");
				}
						
			} catch (MalformedURLException e) {
				e.printStackTrace();
				System.out.println("没网络!请检查您的网络连接!");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("打开网络失败!");
			}finally{
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	
		return buffer.toString();
	}
	
	//java入口
	public static void main(String[] args) {
		
		String url = "http://www.ifeng.com/";
		String encoding = "gbk";
		String filePath="G:\\BaiduYunDownload\\";   
		
		//根据url网址和页面的编码机,获取网页的源代码
		String html = getHtmlResourceByUrl(url, encoding);
		//解析源代码
		Document document = Jsoup.parse(html);
		//获取图片的地址
		Elements elements = document.getElementsByTag("img");
		
		for(Element element : elements){
			String imgSrc = element.attr("src");
			System.out.println(imgSrc);
			if(!"".equals(imgSrc) && imgSrc.startsWith("http://")){
				downImage( filePath,imgSrc);
			}
		}
			
	}
		
}


你可能感兴趣的:(java 网页图片抓取)