Jsoup - 从元素中提取 html
Jsoup - extract html from element
我想使用 jsoup HTML 解析器库从 div 元素中提取 HTML 代码。
HTML代码:
<div class="entry-content">
<div class="entry-body">
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
<p><em>Text 2</em> </p>
</div>
</div>
摘录部分:
String content = ... the content of the HTML from above
Document doc = Jsoup.parse(content);
Element el = doc.select("div.entry-body").first();
我希望结果 el.html()
是 div 选项卡条目正文中的整个 HTML:
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
<p><em>Text 2</em> </p>
但我只得到第一个 <p>
标签:
<p><strong>Text 1</strong></p>
试试这个:
Elements el = doc.select("div.entry-body");
而不是这个:
Element el = doc.select("div.entry-body").first();
然后:
for(Element e : el){
e.html();
}
编辑
如果你这样做,也许你会得到你的结果:
我已经尝试这样做并且它给出了正确的结果。
Elements el = doc.select("a.asset-img-link");
在你的情况下,你会使用
doc.select("div[name=entry-body]") to select that specific <div>
根据这个cookbook
正如对 OP 的评论中提到的,我不明白。这是我对问题的重现,它完全符合您的要求:
String html = ""
+"<div class=\"entry-content\">"
+" <div class=\"entry-body\">"
+" <p><strong>Text 1</strong></p>"
+" <p><strong> <a class=\"asset-img-link\" href=\"http://example.com\" style=\"display: inline;\"><img alt=\"IMG_7519\" class=\"asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive\" src=\"http://example.com\" style=\"width: 500px;\" title=\"IMG_7519\" /></a><br /></strong></p>"
+" <p><em>Text 2</em> </p>"
+" </div>"
+"</div>"
;
Document doc = Jsoup.parse(html);
Element el = doc.select("div.entry-body").first();
System.out.println(el.html());
这导致以下输出:
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519"></a><br></strong></p>
<p><em>Text 2</em> </p>
我想使用 jsoup HTML 解析器库从 div 元素中提取 HTML 代码。
HTML代码:
<div class="entry-content">
<div class="entry-body">
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
<p><em>Text 2</em> </p>
</div>
</div>
摘录部分:
String content = ... the content of the HTML from above
Document doc = Jsoup.parse(content);
Element el = doc.select("div.entry-body").first();
我希望结果 el.html()
是 div 选项卡条目正文中的整个 HTML:
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
<p><em>Text 2</em> </p>
但我只得到第一个 <p>
标签:
<p><strong>Text 1</strong></p>
试试这个:
Elements el = doc.select("div.entry-body");
而不是这个:
Element el = doc.select("div.entry-body").first();
然后:
for(Element e : el){
e.html();
}
编辑
如果你这样做,也许你会得到你的结果:
我已经尝试这样做并且它给出了正确的结果。
Elements el = doc.select("a.asset-img-link");
在你的情况下,你会使用
doc.select("div[name=entry-body]") to select that specific <div>
根据这个cookbook
正如对 OP 的评论中提到的,我不明白。这是我对问题的重现,它完全符合您的要求:
String html = ""
+"<div class=\"entry-content\">"
+" <div class=\"entry-body\">"
+" <p><strong>Text 1</strong></p>"
+" <p><strong> <a class=\"asset-img-link\" href=\"http://example.com\" style=\"display: inline;\"><img alt=\"IMG_7519\" class=\"asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive\" src=\"http://example.com\" style=\"width: 500px;\" title=\"IMG_7519\" /></a><br /></strong></p>"
+" <p><em>Text 2</em> </p>"
+" </div>"
+"</div>"
;
Document doc = Jsoup.parse(html);
Element el = doc.select("div.entry-body").first();
System.out.println(el.html());
这导致以下输出:
<p><strong>Text 1</strong></p>
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519"></a><br></strong></p>
<p><em>Text 2</em> </p>