gpt4 book ai didi

java - 如何在Jsoup中关闭自动生成关闭标签?

转载 作者:行者123 更新时间:2023-12-01 12:15:28 27 4
gpt4 key购买 nike

最佳答案

--更新!!

How to prevent tags replacement?中所示

这个问题有一个很好的解决方案:

解析:

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

将给出:

<a href="#Item1"> <p style="font-family:times;margin-top:12pt;margin-left:0pt;"> <font size="2">Item&nbsp;1.</font> </p></a>

谢谢@user2784201!

-- 旧回复:

我不确定你所要求的是否可能,但我认为这违背了 JSoup 以尽可能类似于浏览器的方式解析 html 的哲学。

请注意,浏览器也会关闭该 A 标记。我认为这是因为在 HTML4 中禁止将 P 放在 A 中。看看这个https://stackoverflow.com/a/1828032/3324704

顺便说一句,我认为您使用的是旧版本的 JSoup,如果您使用 1.8.1,您将看到内部 A 标记(由 JSoup 放置在那里的虚假标记,也由浏览器放置)将保留 href。这个事实可能会对您的解析有所帮助。查看 JSoup 1.8.1 的输出(注意内部 <a href="#Item1"> ):

JSOUP PARSED = 
<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="#Item1"> </a>
<p style="font-family:times;margin-top:12pt;margin-left:0pt;"><a href="#Item1"> <font size="2">Item&nbsp;1.</font> </a></p>
</body>
</html>

此外,我还尝试过其他库。 Htmlcleaner ( here ) 引发错误 (a - UnpermissedChild) 并给出非常相似的输出:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<head></head>
<body><a href="#Item1">
</a><p style="font-family:times;margin-top:12pt;margin-left:0pt;"><a href="#Item1">
<font size="2">Item 1.</font>
</a></p></body></html>

jtidy (here) 说:

Warning: missing </a> before <p>

并给出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator"
content="HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net" />
<title></title>
</head>
<body>
<a href="#Item1"></a>
<p style="font-family:times;margin-top:12pt;margin-left:0pt;"><font
size="2">Item&nbsp;1.</font> </p>
</body>
</html>

也许您可以使用常规的 XML 解析器...

抱歉,内容冗长,回复不令人满意:(

关于java - 如何在Jsoup中关闭自动生成关闭标签</tagName>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27040626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com