我想使用 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();
}
编辑
如果你这样做,也许你会得到你的结果:
我已经尝试这样做并且它给出了正确的结果。
元素 el = doc.select("a.asset-img-link");
关于android - Jsoup - 从元素中提取 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264629/