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,此处省略很多字... 总之,如果你想做一件事情的话就尽快去做吧,千万不要把战线拉得太长 ...
随机推荐
- Web的大趋势:Java+大前端
前后端分离,是目前Web开发的主流模式.而Java无疑是后端开发的王者,PHP和.NET目前仍处于水深火热之中,更像是在夹缝中求生存.而大前端,强势崛起!Java+大前端这一强强组合,面对其他Web领 ...
- DEVOPS技术实践_14:使用docker部署jenkins
一 基础环境准备 [root@node6 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node6 ~]# yum -y ...
- maven安装与常用命令
maven安装: 下载地址http://maven.apache.org/download.cgi 1.安装好Java,配置好Java的环境变量(JDK) 2.下载apache-maven-3.5.2 ...
- jenkins安装自动部署
1.1简介: 开源项目,一个可扩展的持续集成引擎.提供了一种易于使用的持续集成系统,能实施监控集成中存在的错误,提供详细的日志文件和提醒功能,还能用图表的形式形象地展示项目构建的趋势和稳定性.还做到持 ...
- 根据设备id自动打开本设备的串口
对于串口设备经常遇到重新拔插串口设备时候,程序又需要重新选择串口打开.对此很少麻烦的要死. 现在我们可以根据该设备的id去遍历串口设备,一旦符合就打开此串口即可. public void init() ...
- JAVA字节码文件之结构
开发工具:IEDA.JDK1.8.WinHex 一.字节码文件结构 源代码 package com.jalja.java.bytecode; /** * @Auther: XL * @Date: 20 ...
- 31.用python中的serial向串口发送和接收数据(案例一)
代码功能说明:1.向串口助手发送十六进制数据:0X01,0X03,0X00,0X00,0X00,0X01,0X84,0X0A: 2.用串口助手向代码发送数据,并将发送过来的数据保存在数据库中,按数据和 ...
- ocx控件的坑
前言 这还是第一次写博客,以前太懒了,现在发现是很有必要记录下这些经验和问题的.最近项目中有个需求(报表单据需要客户签名,连接签字板,把签名单据同步到服务器上),需要和硬件交互,当时硬件商提供了ocx ...
- ASP.NET Core Web程序托管到Windows 服务
前言 在 .NET Core 3.1和WorkerServices构建Windows服务 我们也看到了,如何将workerservices构建成服务,那么本篇文章我们再来看看如何将web应用程序托管到 ...
- Linux学习_菜鸟教程_3
我是在UBANTO上运行Linux的,开机启动时按下shift或者Esc都不能进入到grub,没有百度到可靠的教程. 暂时先这样吧.免得我把系统搞坏了,先学点实用的知识~~ Next Chapter