开发者

Jsoup Element.text() intermittent?

In the following snippet of code:

  String linkHref = "";
  String linkText = "";
  Elements links = div.getElementsByTag("a");
  for (Element link : links) {
    linkHref = link.attr("href");
    linkText += link.text();              
    break;
  }           

linkText is sometimes empty, even when I can see clearly on the WebView that the link text is there!

On the other hand, linkHref always ends up with the correct value.

What could possibly explain this seemingly intermittent behavior?

Is this a bug in Jsoup? Something else that I may be missing?

Update, answering @ BalusC's questions below: The Jsoup version is jsoup-1.5.2 and div.html() says:

<div class="d2 dl"> 
 <a href="nextp.html" class="cO"><img src="images/no001.jpg" alt="" vspace="0" width="69" border="0" height="69" hspace="0" /></a> 
 <span class="bc">2.</span> 
 <a accesskey="2" href="nextp.html"> Subject line </a> 
</div> 
<p class="aG">Hum开发者_JAVA百科an resource policies are viewed as a valuable to understand the companies.</p> 
<div> 
</div>


The first link doesn't contain text at all. It contains an image. So Jsoup is doing its job perfectly fine.

You probably want to make use of the Element#hasText() first to check if the link has text.

if (link.hasText()) {
    linkText += link.text();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜