【问题标题】:JSOUP: Issue with HTML symbol ¨JSOUP:HTML 符号问题 ¨
【发布时间】:2016-04-21 13:06:01
【问题描述】:

以下是传递给 JSOUP 的 HTML 字符串

<p id="pid">&uml;This is string using for testing</p>

Document doc = Jsoup.parse(htmlString);
String text = doc.getElementById("pid").text();

提取属性文本后如下是结果

¨This is string using for testing

但问题是 ¨This 添加了隐藏的十六进制字符。如果我在记事本++ hexEditor ¨This 中查看字符串为c2a854686973 (¨This)

【问题讨论】:

  • 这更像是文本编码问题而不是 HTML 问题吗?最好检查所有内容是否使用相同的文本编码。如果编码不匹配,通常会出现杂散字符。
  • 我正在使用 doc.getElementById("color").text() 文本方法正在返回特定节点的未编码文本。那么还有其他方法可以避免吗?
  • c2a8 就是 unicode 字符 is encoded 的样子。如果您在记事本++ 中将“编码”设置为ANSI,它将消失。您在使用过程中有什么具体问题吗?

标签: java html-parsing jsoup


【解决方案1】:

但问题是 ¨This 添加了隐藏的十六进制字符。

您可以更改加载 HTML 代码的方式。只要您提供字符集名称,Jsoup 就接受解析 InputStream

样本

String s = "<p id=\"pid\">&uml;This is string using for testing</p>";
Document doc = Jsoup.parse(new ByteArrayInputStream(s.getBytes()), "ASCII", "");
System.out.println(doc);

输出

<html>
 <head></head>
 <body>
  <p id="pid">&uml;This is string using for testing</p>
 </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 2014-12-26
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多