[英]Jsoup: Extracting innertext from anchor tag
Here's my problem. 这是我的问题。 I have a html content: innerText I need to extract the "innerText". 我有一个html内容:innerText我需要提取“ innerText”。 While trying this in Jsoup I found that the innertext goes outside the anchor tag when parsed by Jsoup. 在Jsoup中尝试此操作时,我发现当由Jsoup解析时,内部文本超出了定位标记。
Here's my code 这是我的代码
Document doc=Jsoup.parse("<div> <a href="#"> innerText </a> </div>");
System.out.println(doc.html());
output: 输出:
<html>
<head></head>
<body>
<div >
<a href="#"></a>innerText
</div>
</body>
</html>
why is "innerText" moved outside the anchor tag? 为什么“ innerText”移到锚标记之外?
You can access the text by calling the text()
method on the element. 您可以通过在元素上调用text()
方法来访问文本。
Document doc = Jsoup.parse("<div> <a href=\"#\"> innerText </a> </div>");
System.out.println(doc.html());
Elements rows = doc.getElementsByTag("a");
for (Element element : rows) {
System.out.println("element = " + element.text());
}
btw. 顺便说一句 Using your posted code (and JSoup 1.8.1) produces the following output 使用您发布的代码(和JSoup 1.8.1)产生以下输出
<html>
<head></head>
<body>
<div>
<a href="#"> innerText </a>
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.