crawler4j抓取页面 crawler4j抓取页面使用jsoup解析html时的解决办法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

crawler4j抓取页面 crawler4j抓取页面使用jsoup解析html时的解决办法

  2021-03-19 我要评论
想了解crawler4j抓取页面使用jsoup解析html时的解决办法的相关内容吗,在本文为您仔细讲解crawler4j抓取页面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:crawler4j,抓取页面,jsoup解析html,下面大家一起来学习吧。

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

}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们