package httpClient.client;

 import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID; import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
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 HttpClinet { public static void main(String[] args) throws ClientProtocolException, IOException {
// 图片路径
String url = "https://www.mzitu.com/";
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClinet t = new HttpClinet();
HttpEntity httpEntity = t.getEntity(httpClient, url);
String html = EntityUtils.toString(httpEntity, "UTF-8");
Document document = Jsoup.parse(html);
// 像js一样,通过标签获取title
// System.out.println(document.getElementsByTag("title").first());
// 像js一样,通过id 获取文章列表元素对象
Element postList = document.getElementById("pins");
// 像js一样,通过class 获取列表下的所有博客
Elements postItems = postList.select("li a");
// 循环处理每篇博客
String s = "0";
for (Element postItem : postItems) {
String urls = postItem.attr("href").trim();
if (!s.equals(urls)) {
s = urls;
HttpEntity httpEntitys = t.getEntity(httpClient, urls);
String htmls = EntityUtils.toString(httpEntitys, "UTF-8");
Document documents = Jsoup.parse(htmls);
String postLists = documents.getElementsByClass("main-image").first().select("p a img").attr("src");
if (postLists != null) {
System.out.println(postLists);
t.save(postLists, httpClient);
}
}
}
t.close(httpClient);
} public void save(String url, CloseableHttpClient httpClient) throws ClientProtocolException, IOException {
String fileName = url.substring(url.lastIndexOf("."), url.length());
HttpEntity entity = this.getEntity(httpClient, url); // 获取返回实体
if (entity != null) {
System.out.println("Content-Type:" + entity.getContentType().getValue());
InputStream inputStream = entity.getContent();
// 文件复制,common io 包下,需要 引入依赖
FileUtils.copyToFile(inputStream, new File(UUID.randomUUID() + fileName));
}
} public void close(CloseableHttpClient httpClient) throws IOException {
if (httpClient != null) {
httpClient.close();
}
} public HttpEntity getEntity(CloseableHttpClient httpClient, String url) throws ClientProtocolException, IOException {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("If-None-Match", "W/\"5cc2cd8f-2c58");
httpGet.setHeader("Referer", "http://www.mzitu.com/all/");
httpGet.setHeader("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
CloseableHttpResponse response = httpClient.execute(httpGet);
return response.getEntity();
}
}
pom.xml
<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>httpClient</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>client</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>

httpClient爬虫的更多相关文章

  1. pdf.js跨域加载文件

    pdf.js一个基于Html的工具类,熟悉pdf.js的朋友们很清楚,pdf.js帮助我们做了很多事.尤其金融类网站会产生很多的报表.需要在线预览.pdf.js绝对是我们的首选 本地预览 在pdf.j ...

  2. [Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图

    第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果 ...

  3. 使用 HttpClient 和 HtmlParser 实现简易爬虫

    这篇文章介绍了 HtmlParser 开源包和 HttpClient 开源包的使用,在此基础上实现了一个简易的网络爬虫 (Crawler),来说明如何使用 HtmlParser 根据需要处理 Inte ...

  4. HtmlParser + HttpClient 实现爬虫

    简易爬虫的实现 HttpClient 提供了便利的 HTTP 协议访问,使得我们可以很容易的得到某个网页的源码并保存在本地:HtmlParser 提供了如此简便灵巧的类库,可以从网页中便捷的提取出指向 ...

  5. [转]使用 HttpClient 和 HtmlParser 实现简易爬虫

    http://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/ http://blog.csdn.net/dancen/article/d ...

  6. HttpClient的使用-爬虫学习1

    HttpClient的使用-爬虫学习(一) Apache真是伟大,为我们提供了HttpClient.jar,这个HttpClient是客户端的http通信实现库,这个类库的作用是接受和发送http报文 ...

  7. HttpClient和 HtmlParser实现爬虫

    网络爬虫技术 1       什么叫网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不 ...

  8. 用HttpClient和用HttpURLConnection做爬虫发现爬取的代码少了的问题

    最近在学习用java来做爬虫但是发现不管用那种方式都是爬取的代码比网页的源码少了很多在网上查了很多都说是inputStream的缓冲区太小而爬取的网页太大导致读取出来的网页代码不完整,但是后面发现并不 ...

  9. 使用HttpClient和Jsoup实现一个简单爬虫

    一直很想了解一下爬虫这个东西的,完全是出于兴趣,其实刚开始是准备用python的,但是由于种种原因选择了java,此处省略很多字... 总之,如果你想做一件事情的话就尽快去做吧,千万不要把战线拉得太长 ...

随机推荐

  1. 本地项目推送到coding

      当我们本地新建了一个项目,需要放到coding上维护时,按照下面步骤即可做到. 1.先在coding上新建一个项目,并完成初始化. 2.进入到本地项目的目录下 //初始化本地仓库 a:git in ...

  2. java项目中rmi远程调用实例

    1.创建一个学生实体类Student.java: package com.RMIdemo.entity; @SuppressWarnings("serial") public cl ...

  3. 小白进阶之路-python数据类型

    1.数据类型:变量值是我们存储的数据,所以数据类型值得就是变量的不同种类 2.数据分类型的原因:变量值是用来保存现实世界的中的状态的,呢么针对不同的状态就应该用不同类型上午数据去表示 (1)整型int ...

  4. 扫描器是如何判定有xss漏洞的

    这个问题,看似简单,实则触及到很多人的知识盲区 我们都知道,弹窗就能判定这个页面存在xss, 那么扫描器是怎么判断的呢,或者说扫描器是怎么判断是否弹窗的呢 测试发现 当响应的头中content-typ ...

  5. 【转】ArcGIS Server 站点架构-Web Adaptor

    GIS 服务器内置了Web服务器,如果我想用我自己企业内部的服务器,该怎么做? 多个GIS服务器集群又如何做? …… 有问题,说明我们在思考,这也是我们希望看到的,因为只有不断的思考,不断的问自己为什 ...

  6. Visual Studio 2019移除/禁用Live Share按钮

    Visual Studio 2019新增了几个按钮,搜索按钮,Live Share按钮,发送反馈按钮,管理员按钮(即 “Live Share”, “Send Feedback” ,“ADMIN”) L ...

  7. Scala实践6

    1  if表达式 Scala中if...else..表达式是有返回值的,如果if和else返回值类型不一样,则返回Any类型. scala> val a3=10 a3: Int = 10 sca ...

  8. 哪些工具可以提升PHP开发效率

    本文就我自己在开发过程中的一点经验,谈谈如何利用工具来提升开发工作的编码效率, IDE(phpstorm 收费) 一个好的IDE真的可以给开发人员节省大量的时间,我从最开始使用editplus 到su ...

  9. Spirng Boot2 系列教程(二十二)| 启动原理

    一个读者,也是我的好朋友投稿的一篇关于 SpringBoot 启动原理的文章,才大二就如此优秀,未来可期. 我一直想了解一下 SpirngBoot 的是如何启动的,我想就来写一篇关于 SpirngBo ...

  10. Linux删除文件 清除缓存

    相信很多测试 经常会经历开发叫你清除缓存这种事. 那我们要怎么清呢? 一.首先,确认你要清除的缓存在哪个目录下,然后切换到该目录下,比如 我现在知道我的的缓存目录是在newerp这个目录下,则如图 二 ...