我正在尝试使用 Xpath 和 JAVA 来解析 HTML 页面。这是我的代码:
/** Cleaning the html file */
/** the 'doc' variable is a String containing the whole html file */
TagNode tagNode = new HtmlCleaner().clean(doc);
Document doc2 = new DomSerializer( new CleanerProperties() ).createDOM(tagNode);
XPath xpath = XPathFactory.newInstance().newXPath();
/** xpath request */
Object dates_experience = xpath.evaluate("/html/body/div[3]/div/div/div[2]/div/div/div[2]/div[4]/div/div[3]/h4/span[2]", doc2, XPathConstants.NODESET);
NodeList nodes = (NodeList) dates_experience;
String s;
for (int i = 0; i < nodes.getLength(); i++) {
s = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(nodes.item(i).getTextContent());
System.out.println(s);
}
我想我有 stringEscapeUtils 或 HtmlCleaner 的探针,因为在输出中,我看到了这个:
�
而不是那些字符:
é, è, ', à, û, ...etc
例如,我有以下输入:
décembre 2010 - décembre 2010)
février 2010 - juin 2010)
juillet 2009 - septembre 2009)
juin 2009 - juin 2009)
juillet 2008 - août 2008)
我的程序产生以下输出:
d�cembre 2010 - d�cembre 2010)
f�vrier 2010 - juin 2010)
juillet 2009 - septembre 2009)
juin 2009 - juin 2009)
juillet 2008 - ao�t 2008)
你能帮我解决这个问题吗?
谢谢。
最佳答案
我怀疑你应该*un*转义,而不是转义它们:StringEscapeUtils.unescapeHtml4(String)
关于Java - 转换 Html 特殊字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16984692/