crawler4j抓取页面使用jsoup解析html时的解决方法
更新时间:2014年04月08日 09:11:07 作者:
crawler4j对response没有指定编码的页面,解析成乱码,很让人烦恼,下面给出解决方法,需要的朋友可以参考下
crawler4j对已有编码的页面抓取效果不错,用jsoup解析,很多会jquery的程序员都可以操作。但是,crawler4j对response没有指定编码的页面,解析成乱码,很让人烦恼。在找了苦闷之中,无意间发现一年代已久的博文,可以解决问题,修改 Page.load() 中的 contentData 编码即可,这让我心中顿时舒坦了很多,接下来的问题都引刃而解了。
复制代码 代码如下:
public void load(HttpEntity entity) throws Exception {
contentType = null;
Header type = entity.getContentType();
if (type != null) {
contentType = type.getValue();
}
contentEncoding = null;
Header encoding = entity.getContentEncoding();
if (encoding != null) {
contentEncoding = encoding.getValue();
}
Charset charset = ContentType.getOrDefault(entity).getCharset();
if (charset != null) {
contentCharset = charset.displayName();
}else{
contentCharset = "utf-8";
}
//源码
//contentData = EntityUtils.toByteArray(entity);
//修改后的代码
contentData = EntityUtils.toString(entity, Charset.forName("gbk")).getBytes();
}
相关文章
spring Mvc配置xml使ResponseBody返回Json的方法示例
这篇文章主要给大家介绍了关于spring Mvc配置xml使ResponseBody返回Json的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2018-04-04
mybatis使用@mapkey获取的结果的键(key)为null问题
这篇文章主要介绍了mybatis使用@mapkey获取的结果的键(key)为null问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-06-06


最新评论