JAVA简单网络爬虫:爬取一个网站的图片
案例
爬取网站首页的所有图片,本文选取的网站是https://www.splitshire.com
分析
通过分析一个图片可以推导出类似图片。这里以随机选的一个图片为案例。
这里的图片是小图,可以点击图片查看大图,点击NETWORK可以看到大图的请求地址。如下图
所需要的大图和小图地址都得到了
//小图
https://3hsyn13u3q9dhgyrg2qh3tin-wpengine.netdna-ssl.com/wp-content/uploads/2018/06/SplitShire-08114-uai-258x172.jpg
//大图
https://www.splitshire.com/wp-content/uploads/2018/06/SplitShire-08114.jpg
通过分析页面元素可以得到图片的源代码所在位置如下,发现小图的地址是img的src属性,大图的地址是img的data-guid属性。可以通过CSS选择器获得一组图片元素,然后通过属性名获取属性值,也就是图片地址。
<div class="t-entry-visual-cont">
<div class="dummy" style="padding-top: 66.7%;"></div>
<a tabindex="-1" href="https://www.splitshire.com/view-on-lake/" class="pushed" target="_self" data-lb-index="7">
<div class="t-entry-visual-overlay">
<div class="t-entry-visual-overlay-in style-color-wayh-bg" style="opacity: 0.01;"></div>
</div>
<div class="t-overlay-wrap">
<div class="t-overlay-inner">
<div class="t-overlay-content">
<div class="t-overlay-text single-block-padding"></div>
</div>
</div>
</div>
<!-- 发现小图的地址是img的src属性,大图的地址是img的data-guid属性-->
<img class="adaptive-async adaptive-fetching"
src="https://3hsyn13u3q9dhgyrg2qh3tin-wpengine.netdna-ssl.com/wp-content/uploads/2018/11/SplitShire-18-07454-uai-258x172.jpg"
width="258" height="172" alt="View On Lake" data-uniqueid="82583-116059"
data-guid="https://www.splitshire.com/wp-content/uploads/2018/11/SplitShire-18-07454.jpg"
data-path="2018/11/SplitShire-18-07454.jpg" data-width="1700" data-height="1133" data-singlew="4"
data-singleh="4" data-crop="" data-fixed="">
</a>
</div>
实现
使用httpclient和Jsoup进行爬取。
-
导包
使用maven工程,所以修改pom.xml文件进行导包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zmysna</groupId>
<artifactId>day44_httpclient</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--JUnit 的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--导入 httpclient 依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!--导入 httpcore 依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
<!--导入 commons-io 依赖 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<!--jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>0.3.2</version>
</dependency>
</dependencies>
</project>
- java代码
package com.zmysna;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
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.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class getPagesPic {
public static void main(String[] args) throws IOException {
//创建一个URL
URL url = new URL("https://www.splitshire.com/?upage=2");
//得到一个文档对象
Document document = Jsoup.parse(url, 1000000);
//用CCS选择器获取一组图片的元素
Elements elements = document.select(".t-entry-visual-cont a .adaptive-async");
//遍历并下载图片到本地
for (Element element : elements) {
downLoadPic("src", element);
downLoadPic("data-guid", element);
}
}
/**
* 下载图片
* @param attr
* @param element
* @throws IOException
*/
private static void downLoadPic(String attr, Element element) throws IOException {
//获取请求的地址
String src = element.attr(attr);
//创建一个GET请求对象,参数是请求的地址
HttpGet get = new HttpGet(src);
//打印地址(可以在控制台查看下载进度)
System.out.println(src);
//创建一个Http客户端对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//执行get请求,获得响应对象
CloseableHttpResponse res = httpClient.execute(get);
//返回状态为200则代表通信正常
if (res.getStatusLine().getStatusCode() == 200) {
//截取部分地址作为文件名
String name = src.substring(src.lastIndexOf("/") + 1);
//获取实体数据对象
HttpEntity entity = res.getEntity();
//读取内容,获得资源输入流
InputStream in = entity.getContent();
//创建一个输出流
FileOutputStream out = null;
if(attr.equals("src")) {
//src代表小图片地址,把资源放在小图片目录下
out = new FileOutputStream("e:/temp/smallPic/" + name);
}else{
out = new FileOutputStream("e:/temp/bigPic/" + name);
}
//使用IOUtils复制流
IOUtils.copy(in, out);
//关闭流
in.close();
out.close();
}
}
}