Java爬虫实现图片下载

实现爬虫爬取页面的信息所需要的jar包
我是建立maven工程,pom添加的依赖信息

 - <dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
		<version>4.5.3</version>
	</dependency>
 - <dependency>
		<groupId>org.jsoup</groupId>
		<artifactId>jsoup</artifactId>
		<version>1.10.3</version>
	</dependency>
    
	- <dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.7</version>
	</dependency>
	- <dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.6</version>
	</dependency>
爬虫的代码如下,这里是爬取: http://news.4399.com/gonglue/wzlm/pifu/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
//获取这个页面
public class Test {
	@SuppressWarnings("null")
	public static String gethtml(String html) throws IOException {
		// 创建http请求
		CloseableHttpClient createDefalt = HttpClients.createDefault();
		HttpGet get = new HttpGet(html);
		String resulthtml = "";
		CloseableHttpResponse response = null;
		try {
			response = createDefalt.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				resulthtml = EntityUtils.toString(response.getEntity(), "gb2312");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response == null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			createDefalt.close();
		}
		return resulthtml;
	}
通过Document来解析html页面
//通过Document解析页面
	public static void parsHtml(String html) throws IOException {
		Document document = Jsoup.parse(html);
		Element list = document.getElementById("hreo_list");
		Elements select = list.select("img");
		for (Element element : select) {
			String url = element.attr("lz_src");
			String fileName = element.attr("alt");
			downImage(url, fileName);
			System.out.println(fileName);
			System.out.println("下载成功");
		}
	}

下面是通过io流来对页面的图片的下载

static String file = "D://pifu";//下载的目标路径
	public static void downImage(String imgurl, String fileName) {
		//判断目标文件夹是否存在
		File files = new File(file);
		if (!files.exists()) {
			files.mkdirs();
		}

		InputStream is;
		FileOutputStream out;
		try {
			URL url = new URL(imgurl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			is = connection.getInputStream();
			// 创建文件
			File fileofImg = new File(file + "/" + fileName + ".gpg");
			out = new FileOutputStream(fileofImg);
			int i = 0;
			while ((i = is.read()) != -1) {
				out.write(i);
			}
			is.close();
			out.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

运行代码

	public static void main(String[] args) throws IOException {
		String html = gethtml("http://news.4399.com/gonglue/wzlm/pifu/");
		parsHtml(html);
	}

运行结果
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值