Java+爬虫+爬取图片+完整案例+源码
使用Java语言实现爬虫,用来爬取某个网站上的图片
Httpclient负责发送请求
Jsoup负责解析
完整案例
可直接运行
有爬虫思路解析图
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-17 22:15 Java爬虫爬取图片
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgs
文件 836 2018-06-17 18:56 Java爬虫爬取图片GetImgs.classpath
文件 1040 2018-06-17 18:56 Java爬虫爬取图片GetImgs.project
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgs.settings
文件 567 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settings.jsdtscope
文件 364 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.jdt.core.prefs
文件 473 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.common.component
文件 345 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.common.project.facet.core.xml
文件 49 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.jsdt.ui.superType.container
文件 6 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.jsdt.ui.superType.name
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContent
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentmeta-INF
文件 39 2018-06-17 18:56 Java爬虫爬取图片GetImgsWebContentmeta-INFMANIFEST.MF
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentWEB-INF
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentWEB-INFlib
文件 61829 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibcommons-logging-1.2.jar
文件 766123 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibhttpclient-4.5.5.jar
文件 325491 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibhttpcore-4.4.9.jar
文件 395748 2018-06-17 19:26 Java爬虫爬取图片GetImgsWebContentWEB-INFlibjsoup-1.11.3.jar
文件 650 2018-06-17 18:56 Java爬虫爬取图片GetImgsWebContentWEB-INFweb.xml
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuild
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuildclasses
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuildclassescom
文件 6266 2018-06-17 22:06 Java爬虫爬取图片GetImgsuildclassescomImgUtil.class
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgssrc
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgssrccom
文件 6591 2018-06-17 22:01 Java爬虫爬取图片GetImgssrccomImgUtil.java
文件 52756 2018-05-01 22:22 Java爬虫爬取图片爬虫思路.jpg
package com;
import java.io.ByteArrayOutputStream;
import java.io.File;
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 java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
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.select.Elements;
/**
*
* @ClassName: ImgUtil
* @Description: TODO(爬虫工具)
* @author Admin
* @date 2018年6月17日
*
*/
public class ImgUtil {
// 定义一个url地址
private static String url = “http://www.netbian.com/desk/20747.htm“;
/**
*
* @title: main @Description: TODO(这个类的入口) @param @param args 参数 @return
* void 返回类型 @throws
*/
public static void main(String[] args) {
String code = null;
ArrayList list = null;
System.out.println(“开始“);
for(int i = 0; i<10; i++){
code = getCodeByUrl(url);//获取源代码
list= getImgListFromCode(code);//获取图片的地址的集合
saveImgList(list);//将图片保存到本地
getNextFromCode(code);//解析源代码获取下一页地址
}
System.out.println(“结束“);
}
/**
*
* @title: getNextFromCode
* @Description: TODO(解析源代码获取下一页地址)
* @param @param code 参数
* @return void 返回类型
* @throws
*/
public static void getNextFromCode(String code){
//解析源代码获取下一页地址
//页面一定是一个Document对象
//创建一个Document对象
Document document = Jsoup.parse(code);
//节点 获取需要的节点
Elements elements = document.select(“div[class=list]“).select(“a“);
int k = elements.size();
for(int i = 0 ; i < k; i++){
String urlNew = elements.get(i).attr(“href“);
if(i == 0){
url = “http://www.netbian.com“ + urlNew;
}
}
}
/**
*
* @title: saveImgList
* @Description: TODO(遍历图片的地址集合 将图片保存到本地)
* @param @param list 参数
* @return void 返回类型
* @throws
*/
public static void saveImgList(ArrayList list){
//遍历图片的地址集合 将图片保存到本地
int k = list.size();
String src = null;//图片的地址
//定义文件
File file = null;
//定义URL对象
URL url = null;
//定义URL的连接
HttpURLConnection httpURLConnection = null;
//创建输入流
InputStream inputStream = null;
//创建文件输出流
FileOutputStream fileOutputStream = null;
try {
for(int i = 0; i < k; i++){
src = list.get(i);
//将src转换成url地址
url = new URL(src);
//建立连接
httpURLConnection = (HttpURLConnection) url.openConnection();
//定义连接方式
httpURLConnection.setRequestMethod(“GET“);
//设置请求的超时信息
httpURLConnection.setConnectTimeout(5000);
//获取文件输入流
inputStream = httpURLConnection.getInputStream();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-17 22:15 Java爬虫爬取图片
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgs
文件 836 2018-06-17 18:56 Java爬虫爬取图片GetImgs.classpath
文件 1040 2018-06-17 18:56 Java爬虫爬取图片GetImgs.project
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgs.settings
文件 567 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settings.jsdtscope
文件 364 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.jdt.core.prefs
文件 473 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.common.component
文件 345 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.common.project.facet.core.xm
文件 49 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.jsdt.ui.superType.container
文件 6 2018-06-17 18:56 Java爬虫爬取图片GetImgs.settingsorg.eclipse.wst.jsdt.ui.superType.name
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContent
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentme
文件 39 2018-06-17 18:56 Java爬虫爬取图片GetImgsWebContentme
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentWEB-INF
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsWebContentWEB-INFlib
文件 61829 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibcommons-logging-1.2.jar
文件 766123 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibhttpclient-4.5.5.jar
文件 325491 2018-06-17 19:25 Java爬虫爬取图片GetImgsWebContentWEB-INFlibhttpcore-4.4.9.jar
文件 395748 2018-06-17 19:26 Java爬虫爬取图片GetImgsWebContentWEB-INFlibjsoup-1.11.3.jar
文件 650 2018-06-17 18:56 Java爬虫爬取图片GetImgsWebContentWEB-INFweb.xm
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuild
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuildclasses
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgsuildclassescom
文件 6266 2018-06-17 22:06 Java爬虫爬取图片GetImgsuildclassescomImgUtil.class
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgssrc
目录 0 2018-06-17 22:15 Java爬虫爬取图片GetImgssrccom
文件 6591 2018-06-17 22:01 Java爬虫爬取图片GetImgssrccomImgUtil.java
文件 52756 2018-05-01 22:22 Java爬虫爬取图片爬虫思路.jpg
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)