【问题标题】:JSOUP removing words which occur more than once from attributesJSOUP 从属性中删除多次出现的单词
【发布时间】:2017-09-19 14:42:03
【问题描述】:

假设内容如下:

<p><img src=\"https://abcd.com/pic.jpg\" alt=\"man with a umbrella on terrace with lots of xyz\" width=\"500\" height=\"400\" /></p>

如果使用以下代码行,JSOUP 会删除在任何属性中出现多次的单词。

Parser parser = Parser.htmlParser();

parser.settings(new ParseSettings(true, true));


Document doc = Jsoup.parse(modifiedContent,"",parser);

<p><img src=\"https://abcd.com/pic.jpg\" alt=\"man with a umbrella on terrace lots of xyz\" width=\"500\" height=\"400\" /></p>

with这个词被删除了。有关如何处理此问题的任何建议

【问题讨论】:

  • 原始内容是HTML吗?还是String
  • 也就是说,您是从 HTML 文件中读取内容吗?

标签: java html parsing jsoup


【解决方案1】:

您的输入 HTML 已转义了初始引号。这意味着,您的 alt 标记的值不是 man with a umbrella on terrace with lots of xyz,而是 "man。在 alt 标记之后,您基本上有多个布尔属性,例如 witha 等。

JSoup 然后剥离重复的布尔属性,因为它们没有效果。您应该将 HTML 更改为正确的格式,不要使用转义引号

<p><img src="https://abcd.com/pic.jpg" alt="man with a umbrella on terrace with  lots of xyz" width="500" height="400" /></p>

在本地运行它并且 System.out-ing 文档会产生正确的值

<html>
 <head></head>
 <body>
  <p><img src="https://abcd.com/pic.jpg" alt="man with a umbrella on terrace with lots of xyz" width="500" height="400"></p>
 </body>
</html>

【讨论】:

  • 感谢您的帮助。 HTML 编辑器也验证了您的观点。我使用 StringEscapeUtils.escapeJava 摆脱了 /"。
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2021-05-09
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多