Java Jsoup解析和嵌套标记
我正在学习Jsoup,并拥有以下HTML:Java Jsoup解析和嵌套标记,java,jsoup,Java,Jsoup,我正在学习Jsoup,并拥有以下HTML: [...] <p style="..."> <!-- div 1 --> Content </p> <p style="..."> <!-- div 2 --> Content </p> <p style="..."> <!-- div 3 --> Content </p> [...] 返回: <htm
[...]
<p style="..."> <!-- div 1 -->
Content
</p>
<p style="..."> <!-- div 2 -->
Content
</p>
<p style="..."> <!-- div 3 -->
Content
</p>
[...]
返回:
<html>
<head>
<title>Title</title>
</head>
<body>
<p style="margin-left:2em">
<span class="one">Text</span>
<span class="two"><span class="nest">Text</span></span>
<span class="three"></span>
</p>
<p style="margin-left:2em">
<span class="one">Text</span>
<span class="two"><span class="nest">Text</span></span>
<span class="three"></span>
</p>
<p style="margin-left:2em">
<span class="one">Text</span>
<span class="two"><span class="nest">Text</span></span>
<span class="three"></span>
<p style="margin-left:2em"></p>
<p style="margin-left:2em"></p>
</p>
</body>
</html>
Document doc = null;
doc = Jsoup.connect(URL_with_HTML).get();
System.out.println(doc.outerHtml());
<html>
<head>
<title>Title</title>
</head>
<body>
<p style="margin-left:2em"> <span class="one">Text</span> <span class="two"><span class="nest">Text</span></span> <span class="three"></span> </p>
<p style="margin-left:2em"> <span class="one">Text</span> <span class="two"><span class="nest">Text</span></span> <span class="three"></span> </p>
<p style="margin-left:2em"> <span class="one">Text</span> <span class="two"><span class="nest">Text</span></span> <span class="three"></span> </p>
<p style="margin-left:2em"></p>
<p style="margin-left:2em"></p>
<p></p>
</body>
</html>
标题
文本文本
文本文本
文本
这对吗?我正在使用JSOUP1.6.1。我理解Jsoup应该返回嵌套的段落,而不是以前的返回。Hj,我理解原来的问题。但我认为这是Jsoup的一个bug(不是你的)。因为这是一个简单的例子:
<html>
<head></head>
<body>
<p></p>
<p>
<div></div>
</p>
</body>
</html>
但Jsoup分析如下:
<html>
<head></head>
<body>
<p></p>
<p></p>
<div></div>
<p></p>
</body>
</html>
如果可以,请将此错误归档,以便作者可以修复它:-)
注意:只要一个单词“嗨”,stackoverflow就不允许这样做?嵌套段落在HTML中不存在。上一段自动关闭,因为:
p
标签由以下任一项自动关闭:地址
,文章
,旁白
,块引号
,div
,dl
,字段集
,页脚
,表单
,h1
,h2
,h3
,h4
,,h6
,header
,hgroup
,hr
,main
,菜单
,nav
,ol
,p
,pre
,节
,表
,或ul
。因此变成
p
(即
)且没有相应开始标记的结束标记是解析错误,将替换为
。因此
成为
请务必理解,HTML是无效的,因为您有太多的
,而不是因为“嵌套”段落。嵌套无法发生,因为它们会自动关闭。但是后来的
已经过时,因为“对应的”
之前已经自动关闭了。这似乎是一个bug。如果可以,请将其归档。我添加了带有文件/代码的示例以澄清。我怀疑这不是一个bug。如果更换“可疑”,工作正常。也许,中的是HTML不正确的,Jsoup理解并尝试修复它吗?根据我的经验,Jsoup不关心HTML关键字。我一直在使用Jsoup解析XML,效果很好。您可以使用Jsoup.parse(“”)进行测试。
不允许包含任何块元素!所以这个“简单”的例子是无效的(X)HTML。@FabianBarney,谢谢,我知道了。仅p标记就有类似的问题。当我解析这个HTML示例文本时,我得到了作为
示例文本的输出示例1
示例2非常感谢!HTML代码是第三部分(不是我的),我没有修改它。现在,我如何解决这个问题?(将嵌套段落替换为span class=“p”,例如,如果问题出现在parse()方法中?@FabianBarney,Jsoup不关心有效的HTML标记。请使用
Jsoup.parse(“”
)进行测试。但是Jsoup关心“真实世界”的HTML解析。自动关闭
和
中嵌套块元素的未定义行为是真实的解析。如果使用绝对合法的HTML,如…第一段第二段…
,则会遇到很多麻烦。不关闭
是合法的@Manz jsoup与浏览器非常相似,也进行解析。使用诸如Firebug之类的工具检查页面,您将看到HTML也得到了相同的解释。(自动关闭的p标签等)我们无法告诉您如何修复它,因为不清楚您想要的输出应该是什么样子。如果需要嵌套块元素,则不能将
用作外部标记。用
代替…@FabianBarney,谢谢你,我直到现在才知道。
<html>
<head></head>
<body>
<p></p>
<p>
<div></div>
</p>
</body>
</html>
<html>
<head></head>
<body>
<p></p>
<p></p>
<div></div>
<p></p>
</body>
</html>