欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

crawler4j抓取頁(yè)面使用jsoup解析html時(shí)的解決方法

 更新時(shí)間:2014年04月08日 09:11:07   作者:  
crawler4j對(duì)response沒有指定編碼的頁(yè)面,解析成亂碼,很讓人煩惱,下面給出解決方法,需要的朋友可以參考下

crawler4j對(duì)已有編碼的頁(yè)面抓取效果不錯(cuò),用jsoup解析,很多會(huì)jquery的程序員都可以操作。但是,crawler4j對(duì)response沒有指定編碼的頁(yè)面,解析成亂碼,很讓人煩惱。在找了苦悶之中,無意間發(fā)現(xiàn)一年代已久的博文,可以解決問題,修改 Page.load() 中的 contentData 編碼即可,這讓我心中頓時(shí)舒坦了很多,接下來的問題都引刃而解了。

復(fù)制代碼 代碼如下:

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

}

相關(guān)文章

最新評(píng)論