写在之前

本文章不研究爬虫技术,只做java可以用于获取网页内容的介绍。

认识RestTemplate

RestTemplate是Spring提供的用于访问RESTful服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。RestTemplate默认依赖JDK提供http连接的能力 (HttpURLConnection),也可以通过替换为例如 Apache HttpComponents、Netty或OkHttp等其它HTTP 客户 端,OkHttp的性能优越,本博客使用OkHttp,官网, github。

创建springboot项目

引入OkHttp依赖

<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.9.1</version></dependency>

如果引入了spring-web依赖则不需要引入RestTemplate相关依赖

初始化RestTemplate

在springboot启动类中初始化

@SpringBootApplication
public class MerchantApplicationBootstrap {public static void main(String[] args) {SpringApplication.run(MerchantApplicationBootstrap.class,args);}/***RestTemplate初始化* @return*/@BeanRestTemplate restTemplate(){return new RestTemplate(new OkHttp3ClientHttpRequestFactory());}
}
测试

新建测试类

/*** @Author: 许喜朝* @Date: 2021/1/22 9:53*/
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class RestTemplateTest {@AutowiredRestTemplate restTemplate;@Testpublic void gethtml(){//获取百度首页的网页内容String url = "http://www.baidu.com/";ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);String body = forEntity.getBody();System.out.println(body);}
}

结果如下

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ–°é—»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å
³äºŽç™¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前å¿
读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

细心的小伙伴可能已经发现了,获取到的内容是乱码的
这是因为当RestTemplate默认使用String存储body内容时默认使用ISO_8859_1字符集。

解决乱码问题

配置StringHttpMessageConverter 消息转换器,使用utf-8字符集。

在springboot启动类中修改RestTemplate的配置

/***RestTemplate初始化* @return*/@BeanRestTemplate restTemplate(){RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());//消息转换器列表List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();//配置消息转换器StringHttpMessageConverter,并设置utf‐8messageConverters.set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));//支持中文字符集,默认ISO‐ 8859‐1,支持utf-8return restTemplate;}

有小伙伴可能会问设置消息转换器为什么是1?
这个可以通过debug看看HttpMessageConverte消息转换列表的值

这样一看就很清晰了
重启启动类,从新测试之后乱码问题解决

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

java获取百度网页内容相关推荐

  1. Java百度网盘创建链接,java获取百度网盘真实下载链接的方法

    本文实例讲述了java获取百度网盘真实下载链接的方法.分享给大家供大家参考.具体如下: 目前还存在一个问题,同一ip在获取3次以后会出现验证码,会获取失败,感兴趣的朋友对此可以加以完善. 返回的Lis ...

  2. java获取百度实时天气(无限制)

    前言 因公司需求,需要获取当前登录账号的实时天气信息,网上查找后很多都有次数限制,最后东拼西凑找到了一个可以无限制使用获取百度天气的接口. 一.获取外网IP 代码如下: public static S ...

  3. JAVA获取百度网盘下载真实地址

    这是一个java写的获取百度网盘真实下载链接进行下载的程序.  程序里面一些参数拼接是根据浏览器抓包来的.具体的抓包方法网上一大堆,可以参考.这里给出了源码和导出的jar包.  url网址使用于百度分 ...

  4. java批量获取百度图片

    java获取百度图片学习记录 主要用到的jar包 <dependency><groupId>org.apache.httpcomponents</groupId>& ...

  5. java调用百度地图API依据地理位置中文获取经纬度

    百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...

  6. Java使用百度AI实现识别身份证照片信息,根据身份证号码,获取相关个人信息

    Java使用百度AI实现识别身份证照片信息 百度智能云-登录 1.登录百度智能云,选择文字识别,创建相关信息 2.获取APP_ID.API_KEY.SECRET_KEY 核心处理代码 import c ...

  7. java后台百度地图经纬度和地址之间的相互转换(通过经纬度获取地址、通过地址获取经纬度)

    准备工作: 1.申请百度ak(v1版本的key)  百度地图开放平台链接:http://lbsyun.baidu.com/ 2.百度地图web服务API链接:http://lbsyun.baidu.c ...

  8. Java获取照片的Exif信息,并解析GPS

    Java获取照片的Exif信息,并解析GPS 1. 效果图 2. 什么是Exif? 3. 源代码 参考 ❤️ 这篇博客将介绍什么EXIF,以及如何用Java语言读取Exif信息,并转换经纬度为位置信息 ...

  9. java爬取网页内容 简单例子(2)——附jsoup的select用法详解

    [背景] 在上一篇博文 java爬取网页内容 简单例子(1)--使用正则表达式 里面,介绍了如何使用正则表达式去解析网页的内容,虽然该正则表达式比较通用,但繁琐,代码量多,现实中想要想出一条简单的正则 ...

最新文章

  1. update yum 到指定版本_yum语法及常用命令汇总
  2. azkaban获取上游的节点结果_Flink任务实时获取并更新规则
  3. 1.3:Render Pipeline and GPU Pipeline
  4. 搜狗输入法在idea打不了汉字_IDEA开发软件在linux环境下使用搜狗输入法无法进行中文输入...
  5. sqlserver查看索引_SQL Server页中行物理存储
  6. Flutter MaterialButton 实现圆角边框按钮
  7. 常用选择器(CSS+JQuery)
  8. 中考英语听说计算机考试满分,中考英语听说机考在即,这些考生可免试计满分...
  9. 存储过程游标注意事项——表需要使用别名,如红色字体
  10. 多态和C++多态的实现(汇总)
  11. JS高级程序设计第四版启航
  12. 什么是特洛伊木马病毒?
  13. 输入关键字生成对联_输入真实名字自动生成网名,名字对联自动生成
  14. PHP 获取微视无水印源地址_小红书无水印视频解析下载|小红书在线去水印|小红书视频解析API接口...
  15. 什么是强制性3C认证?
  16. android谷歌地图删除marker,无法将marker从google地图上删除
  17. RSA2048 private key der格式结构
  18. MATLAB画Correlation plots
  19. 节能减排社会实践 与科技竞赛作品申报书
  20. 登录谷歌账号出现浏览器不安全的提示

热门文章

  1. centos7安装OTRS开源工单管理平台
  2. BOF——Bag-of-Featrures
  3. linux下gz和tar.gz、zip压缩解压
  4. 将list对象存储进redis中去
  5. IDEA打包JavaWeb的War包并部署到Tomcat
  6. 仓库温度湿度控制措施_仓库温湿度管理规定_仓库温湿度监测管理制度
  7. 元宇宙012 | 世界人工智能大会之元宇宙论坛:技术篇
  8. 联想电脑thinkPad开机黑屏
  9. Python可视化库matplotlib(超详细)
  10. 三阶交调(三阶互调) IP3