后来,确认问题应该出现在nekohtml(HtmlParser默认使用的html文档解析包)身上,
于是下载nekohtml查看源码,发现默认情况下,如果nekohtml在页面中找到charset的设置,他将使用页面中指定的编码集,所以大量的页面都用页面中指定的GB2312进行解析,这样如果页面中存在GB2312没有包含的字符时就会出现乱码,在HtmlScanner中有一个特征(Feature),可以通过设置其值来关闭这个功能。
/**
* Ignore specified charset found in the <meta * content='text/html;charset=…'>tag. */ public static final String IGNORE_SPECIFIED_CHARSET |
在代码中这样做:
parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset",true);