java entity tostring_EntityUtils.toString(entity)处理字符集问题解决

爬取51job和猎聘网的信息,想处理字符集问题(51job为gbk,猎聘为utf-8),

找到两个网站字符集信息都在同一标签下

就想先把网页保存成string,解析一遍获取字符集,然后将网页转换成对应的正确的字符集,最后再转换成统一的字符集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);

{

报错

233de4263ac331db49dfb00f3125f902.png

参考 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);

{

输出:

674e6dc86cd1c99bced53a39e4189f87.png

字符集依旧有问题,发现不指定字符集,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编码,

只要没有改变英文的编码,就没有影响,只要能从标签里提取出charset字符集就可以转换成正确的

输出正常

0f23db7ba191c3f0c9e838d87a4682a9.png

换成猎聘的url再尝试

0221cbb646eefc95fa61dd634b9531a2.png

完美,爬虫的字符集真神奇

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值