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. 第二阶段:2.商业需求文档MRD:3.MRD-目标用户分析

    以上是对目标市场的分析! 用户描述是定性.市场统计是定量.用用户分类模型去剖析用户分类. 例子.做百度推广时候的用户分析.不同角色的关注点不同.三个情景:广告售卖,广告投放,分析评估. 用户的使用习惯 ...

  2. Alibaba Cloud Toolkit 使用心得(IDEA版)

    一.安装插件 确保 IntelliJ IDEA 在 2018.1 或更高版本 打开 Settings - Plugins 搜索安装 Alibaba Cloud Toolkit 二.配置环境 Deplo ...

  3. 1.2 UML带来了什么(学习笔记)

    需求->需求分析->设计->开发 uml 编号 uml元素 对于语言理解 1 元模型 基本词汇 2 表示法或视图 语法 3 RUP 方法(统一软件开发过程)  方法 4 控制类 定语 ...

  4. Cent OS防火墙配置端口开放

    CentOS 6内置的防火墙为iptables,Cent OS7,内置的防火墙则是firewalld iptables 防火墙设置 1.打开/关闭/重启防火墙 #开启防火墙(重启后永久生效): chk ...

  5. [技术翻译]使用Nuxt生成静态网站

    本周再来翻译一些技术文章,本次预计翻译三篇文章如下: 04.[译]使用Nuxt生成静态网站(Generate Static Websites with Nuxt) 05.[译]Web网页内容是如何影响 ...

  6. linux中的ldd命令简介

    转载自:http://blog.csdn.net/stpeace/article/details/47069215 在linux中, 有些命令是大家通用的, 比如ls, rm, mv, cp等等, 这 ...

  7. Scala与Mongodb实践2-----图片、日期的存储读取

    目的:在IDEA中实现图片.日期等相关的类型在mongodb存储读取 主要是Scala和mongodb里面的类型的转换.Scala里面的数据编码类型和mongodb里面的存储的数据类型各个不同.存在类 ...

  8. Python中函数参数 *args 和 **kwargs

    普通参数,即在调用函数时必须按照准确的顺序来进行参数传递. 默认参数,即参数含有默认值,在调用函数时可以进行参数传递,若没有进行参数传递则使用默认值,要注意,默认参数必须在普通参数的右侧(否则解释器无 ...

  9. sg函数的学习

    1 .anti-nim 2 . 可以拆分的

  10. hdu6638 线段树求最大子段和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 Problem Description There are n pirate chests bu ...