手机
当前位置:查字典教程网 >编程开发 >Java >crawler4j抓取页面使用jsoup解析html时的解决方法
crawler4j抓取页面使用jsoup解析html时的解决方法
摘要:crawler4j对已有编码的页面抓取效果不错,用jsoup解析,很多会jquery的程序员都可以操作。但是,crawler4j对respo...

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();

}

【crawler4j抓取页面使用jsoup解析html时的解决方法】相关文章:

java 格式化输出数字的方法

java正则表达式提取数字的方法实例

Java 获取指定日期的实现方法总结

java中使用sax解析xml的解决方法

分享关于JAVA 中使用Preferences读写注册表时要注意的地方

Java 使用poi把数据库中数据导入Excel的解决方法

java执行bat命令碰到的阻塞问题的解决方法

在java中使用dom解析xml的示例分析

java堆栈类使用实例(java中stack的使用方法)

java中Filter过滤器处理中文乱码的方法

精品推荐
分类导航