crawler4j對已有編碼的頁面抓取效果不錯,用html">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();
}