EntityUtils.toString(entity)处理字符集问题解决
爬取51Job和猎聘网的信息,想处理字符集问题(51job为gbk,猎聘为utf-8),
找到两个网站字符集信息都在同一标签下
就想先把网页保存成String,解析一遍获取字符集(因为charset字符是英文,不需要转换字符集),然后将网页中文内容转换成对应的正确的字符集,最后再转换成统一的字符集utf-8
1.0实现,2次调用Entity.Utils.toString方法
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
//网站转为String
String get_Charset_Entity2String = EntityUtils.toString(httpResponse.getEntity());
//解析
Document get_Charset_Document = Jsoup.parse(get_Charset_Entity2String);
//字符集信息提取,51job和猎聘
String charset = get_Charset_Document.select("meta[http-equiv=Content-Type]")
.attr("content").split("=")[1];
System.out.println(charset);
//根据字符集重新编码成正确的
String Ori_Entity = EntityUtils.toString(httpResponse.getEntity(),charset);
//转换为统一的utf-8
String entity = new String(Ori_Entity.getBytes(),"utf-8");
System.out.println(entity);
{
报错

参考 https://blog.csdn.net/qq_23145857/article/details/70213277
发现EntityUtils流只存在一次,但是有不想一个网页要连接两次,
这难不倒我,直接转换原来保留的String
2.0实现,第二次不使用EntityUtils
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
//网站转为String
String get_Charset_Entity2String = EntityUtils.toString(httpResponse.getEntity());
//解析
Document get_Charset_Document = Jsoup.parse(get_Charset_Entity2String);
//字符集信息提取,51job和猎聘
String charset = get_Charset_Document.select("meta[http-equiv=Content-Type]")
.attr("content").split("=")[1];
System.out.println(charset);
//根据字符集重新编码成正确的,不用EntityUtils,直接转get_Charset_Entity2String
String Ori_Entity = new String(get_Charset_Entity2String.getBytes(), charset);
//转换为统一的utf-8
String entity = new String(Ori_Entity.getBytes(),"utf-8");
System.out.println(entity);
{
输出:

字符集依旧有问题,发现不指定字符集,EntityUtils.toString()就用"ISO-8859-1"字符集,可我就是不知道字符集
看到参考链接下面的解决办法,眼前一亮,把流直接以位数组保存,都能灵活变换
3.0实现,不使用EntityUtils.toString,改用EntityUtils.toByteArray()
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
//网站转换为byte[]
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
//byte列表转为默认字符集
String get_Charset_Entity2String = new String(bytes);
//解析
Document get_Charset_Document = Jsoup.parse(get_Charset_Entity2String);
//字符集信息提取,51job和猎聘
String charset = get_Charset_Document.select("meta[http-equiv=Content-Type]")
.attr("content").split("=")[1];
System.out.println(charset);
//根据字符集重新编码成正确的
String Ori_Entity = new String(bytes, charset);
//转换为统一的utf-8
String entity = new String(Ori_Entity.getBytes(), "utf-8");
System.out.println(entity);
}
对于里面的默认字符集
参考:https://blog.csdn.net/wangxin1949/article/details/78974037
- 1、如果使用了eclipse,由java文件的编码决定
- 2、如果没有使用eclipse,则有本地电脑语言环境决定,中国的都是默认GBK编码,
输出正常

换成猎聘的url再尝试

完美,爬虫的字符集真神奇
EntityUtils.toString(entity)处理字符集问题解决的更多相关文章
- mysqldump之字符集问题解决
最近导数据的需求真多,年底了大家都要做分析吗?近期使用mysqldump导出的中文出现乱码问题. mysqldump -uroot -ppassword -S /tmp/mysql9991.sock ...
- mysql点滴_02程序中运行sql语句报字符集问题解决
程序中运行 "SELECT t.EVENT_TYPE_ID FROM RATABLE_EVENT_TYPE t WHERE t.NAME='帐期末费用转移事件'" 报错 错误码 ...
- HttpClient(4.3.5) - HTTP Entity
HTTP entity HTTP messages can carry a content entity associated with the request or response. Entiti ...
- HttpClient之EntityUtils对象
最近在学习安卓并用thinkphp做后台,为了抵抗自己的烂记性,就在这里记录一下当我从tp后台获取到json串传到安卓客户端所用到的一个方法函数. EntityUtils对象是org.apache.h ...
- 跨域问题解决方式(HttpClient安全跨域 & jsonp跨域)
1 错误场景 今天要把项目部署到外网的时候,出现了这种问题, 我把两个项目放到自己本机的tomcat下, 进行代码调试, 执行 都没有问题的, 一旦把我须要调用接口的项目B放到其它的server上, ...
- HttpClient之EntityUtils工具类
今天看到tttpclient-tutorial上面有这样一句话-----非常的不推荐使用EntityUtils,除非知道Entity是来自可信任的Http Server 而且还需要知道它的最大长度.文 ...
- handler以及AnyscTask处理机制
1.Handler 主文件:MainActivity.java package com.example.asynctaskdownload; import java.io.IOException; i ...
- JavaWeb日常笔记
1. XML文档的作用和解析 1. XML的基本概述: XML的主要是用来存储一对多的数据,另外还可以用来当做配置文件存储数据.XML的表头如下: <?xml version='1.0' e ...
- 阿里云CDN刷新预热接口
阿里云OSS映射的文件地址需要即时访问到最新数据,需要即时调用CDN的刷新预热类接口 RefreshObjectCaches 刷新接口. 参考官方接口文档资料:https://help.aliyun. ...
随机推荐
- 权限管理——ACL权限
权限管理 ACL权限 用于解决用户对文件身份不足 命令:[root@localhost ~]#dumpe2fs -h /dev/sd3 作用:查询指定分区详细的文件系统给信息 选项 -h:仅显示超级块 ...
- JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼?
<JavaScript 深入浅出>系列: JavaScript 深入浅出第 1 课:箭头函数中的 this 究竟是什么鬼? JavaScript 深入浅出第 2 课:函数是一等公民是什么意 ...
- form分辨率
近期做项目时,遇到开发的winform在自己电脑上可以正常显示,共享到其他电脑就事儿不能显示了: [转载自:http://blog.csdn.net/lcawen88/article/details/ ...
- CODING 受邀参加《腾讯全球数字生态大会》
近日,腾讯全年最重要的一场活动--<腾讯全球数字生态大会>于昆明滇池国际会展中心正式举办.此次全球数字生态大会是腾讯战略升级后,整合互联网+数字经济峰会.云+未来峰会.腾讯全球合作伙伴三大 ...
- Quest 公司的Shareplex 与 GoldenGate比较
Quest 公司的Shareplex 与 GoldenGate比较 2012-08-01 16:51:12 —————————————————————————————————————————— ...
- java实现序列化的两种方式
1.Serializable接口 2.Externalizable接口 public class Demo2 implements Externalizable{ transient private ...
- protocol buffers 使用方法
protocol buffers 使用方法 为什么使用 Protocol Buffers 我们接下来要使用的例子是一个非常简单的"地址簿"应用程序,它能从文件中读取联系人详细信息. ...
- prometheus operator的深度好文
转: https://www.servicemesher.com/blog/prometheus-operator-manual/
- 23.Java基础_ArrayList类
ArrayList类的构造和使用方法 import java.util.ArrayList; public class test { public static void main(String[] ...
- Pwnable-blukat
ssh blukat@pwnable.kr -p2222 (pw: guest) 连接上去看看c的源码 #include <stdio.h> #include <string.h&g ...