微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jsoup-<head>中的<noscript>内容被解释为文本

如何解决Jsoup-<head>中的<noscript>内容被解释为文本

我遇到一个问题,将以下HTML结果解析为不需要的结果。

HTML

<html>
<head>
<title>Try jsoup</title>
<noscript><p>thisisatest</p></noscript>
<noscript><img id="tracking-test-noscript" style="width: 1px; height: 1px" src="http://fullwithsheep/img/tracking3.jpg"></noscript>
</head>
<body>
<noscript><p>thisisatest</p></noscript>
<p>This is <a href="http://jsoup.org/">jsoup</a>.</p>
<noscript><img id="tracking-test-noscript" style="width: 1px; height: 1px" src="http://fullwithsheep/img/tracking3.jpg"></noscript>
</body>
</html>

JSOUP对文档的解释

<html>
<head>
<title>Try jsoup</title>
<noscript>&lt;p&gt;thisisatest</noscript>
<noscript>&lt;img  id="tracking-test-noscript" style="width: 1px; height: 1px" src="http://fullwithsheep/img/tracking3.jpg"&gt;</noscript>
</head>
<body>
<noscript><p>thisisatest</p></noscript>
<p>This is <a href="http://jsoup.org/">jsoup</a>.</p>
<noscript><img id="tracking-test-noscript" style="width: 1px; height: 1px" src="http://fullwithsheep/img/tracking3.jpg"></noscript>
</body></html>

enter image description here

您可以从头节点中的noscript标记(解释为文本)中看到innerHTML,我想要的是jsoup仍会将其解释为html而不是文本(不将&lt;等)

作为解决此问题的方法,我做的是在中断Jsoup.parse之后选择所有noscript标记,然后尝试将各个noscript标记的文本转换回html。但是,这感觉不是正确的方法-这是Jsoup库中的错误还是此行为的意图?

解决方法

使用xmlParser避免不必要的HTML修改:

Document doc = Jsoup.parse(html,"",Parser.xmlParser());

默认解析器treats input as HTML5,and enforces the creation of a normalised document,based on a knowledge of the semantics of the incoming tags
而xmlParser assumes no knowledge of the incoming tags and does not treat it as HTML,rather creates a simple tree directly from the input就是您所需要的。

报价来自文档:https://jsoup.org/apidocs/org/jsoup/parser/Parser.html#xmlParser()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。