httpClient爬虫
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爬虫的更多相关文章
- pdf.js跨域加载文件
pdf.js一个基于Html的工具类,熟悉pdf.js的朋友们很清楚,pdf.js帮助我们做了很多事.尤其金融类网站会产生很多的报表.需要在线预览.pdf.js绝对是我们的首选 本地预览 在pdf.j ...
- [Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图
第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果 ...
- 使用 HttpClient 和 HtmlParser 实现简易爬虫
这篇文章介绍了 HtmlParser 开源包和 HttpClient 开源包的使用,在此基础上实现了一个简易的网络爬虫 (Crawler),来说明如何使用 HtmlParser 根据需要处理 Inte ...
- HtmlParser + HttpClient 实现爬虫
简易爬虫的实现 HttpClient 提供了便利的 HTTP 协议访问,使得我们可以很容易的得到某个网页的源码并保存在本地:HtmlParser 提供了如此简便灵巧的类库,可以从网页中便捷的提取出指向 ...
- [转]使用 HttpClient 和 HtmlParser 实现简易爬虫
http://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/ http://blog.csdn.net/dancen/article/d ...
- HttpClient的使用-爬虫学习1
HttpClient的使用-爬虫学习(一) Apache真是伟大,为我们提供了HttpClient.jar,这个HttpClient是客户端的http通信实现库,这个类库的作用是接受和发送http报文 ...
- HttpClient和 HtmlParser实现爬虫
网络爬虫技术 1 什么叫网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不 ...
- 用HttpClient和用HttpURLConnection做爬虫发现爬取的代码少了的问题
最近在学习用java来做爬虫但是发现不管用那种方式都是爬取的代码比网页的源码少了很多在网上查了很多都说是inputStream的缓冲区太小而爬取的网页太大导致读取出来的网页代码不完整,但是后面发现并不 ...
- 使用HttpClient和Jsoup实现一个简单爬虫
一直很想了解一下爬虫这个东西的,完全是出于兴趣,其实刚开始是准备用python的,但是由于种种原因选择了java,此处省略很多字... 总之,如果你想做一件事情的话就尽快去做吧,千万不要把战线拉得太长 ...
随机推荐
- iptables 基础
SNAT 和 DNAT 是 iptables 中使用 NAT 规则相关的的两个重要概念.如上图所示,如果内网主机访问外网而经过路由时,源 IP 会发生改变,这种变更行为就是 SNAT:反之,当外网的数 ...
- 掌握这些,ArrayList就不用担心了!
关于ArrayList的学习 ArrayList属于Java基础知识,面试中会经常问到,所以作为一个Java从业者,它是你不得不掌握的一个知识点.
- 解析GMT+N时区,返回日期类型
涉及到正则表达式,时区转换. /** * * 按格式 yyyy-MM-dd HH:mm:ss 以指定GMT时区进行解析,返回对应的当前系统时区当地时间. * @param dateString 格式 ...
- JAVA的引用类型
一.强引用 JAVA默认的引用类型,强引用,是在我们的开发工作当中普遍存在的.如果一个对象具有强引用,当内存空间不足的时候,java虚拟机宁可抛出OOM异常,也不会回收它来释放内存.但是我们可以将对象 ...
- 「算法竞赛进阶指南」0x01 位运算 知识笔记
二进制是计算机的根本! 你了解她它吗? int lowbit(int x) { return x&(-x);//x&(~x+1),~x=-1-x; } int __builtin_ct ...
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) (前三题题解)
这场比赛好毒瘤哇,看第四题好像是中国人出的,怕不是dllxl出的. 第四道什么鬼,互动题不说,花了四十五分钟看懂题目,都想砸电脑了.然后发现不会,互动题从来没做过. 不过这次新号上蓝名了(我才不告诉你 ...
- bat无法识别命令、无法识别运算符、结果不符合预期等问题
1.无法识别的命令等可能为中文字符编码等问题,破坏了bat文件格式,建议去掉中文,加上引号或者重新创建文件.例如:@echo off%~d0cd /d %~dp0title epoch时间转换(%cd ...
- Spring Cloud Stream消息驱动之RocketMQ入门(一)
SpringCloudStream目前支持的中间件有RabbitMQ.Kafka,还有我最近在学习的RocketMQ,以下是我学习的笔记 学习Spring cloud Stream 可以先学习一下了解 ...
- React-router路由4.0版本用法
第一步:引包两个 命令:cnpm i -S react-router react-router-dom 第二步:用路由管理APP页面 1.创建主路由管理页面,在这里使用了路由嵌套 import Rea ...
- 小白学Java:老师!泛型我懂了!
目录 小白学Java:老师!泛型我懂了! 泛型概述 定义泛型 泛型类的定义 泛型方法的定义 类型变量的限定 原生类型与向后兼容 通配泛型 非受限通配 受限通配 下限通配 泛型的擦除和限制 类型擦除 类 ...