简体   繁体   中英

How to get text from 'ul' tag?

I'm using Java Jsoup to get some details.

<ul class="vcard-details"> 
 <li class="vcard-detail" itemprop="homeLocation"><span class="octicon octicon-location"></span>Caldwell, Idaho, USA</li> 
 <li class="vcard-detail"><span class="octicon octicon-mail"></span><a class="email" href="mailto:jamisbuck@gmail.com">jamisbuck@gmail.com</a></li> 
 <li class="vcard-detail" itemprop="url"><span class="octicon octicon-link"></span><a href="http://blog.jamisbuck.org" class="url" rel="nofollow me">http://blog.jamisbuck.org</a></li> 
 <li class="vcard-detail"><span class="octicon octicon-clock"></span><span class="join-label">Joined on </span><time class="join-date" datetime="2008-02-28T17:37:32Z" day="numeric" is="local-time" month="short" year="numeric" title="Feb 28, 2008, 11:07 PM GMT+5:30">Feb 28, 2008</time></li> 
</ul>

I've got the above section using..

Element bio = doc.getElementsByClass("vcard-details").first();

Is there any way I can get the text ' Caldwell, Idaho, USA ' ? I'm trying to make use of class name 'octicon octicon-location', because I need to get all those attributes seperatly (only if they are available).

The Span with the class you want to use contains no value, since it's closed before the text 'Caldwell, Idaho, Usa' is in the document. To access to contents of the li tag you can use the code below. You can then manipulate the string to get rid of the Span tag.

var value = document.querySelector('li.vcard-detail').innerHTML;

Using jsoup's selector syntax :

Element first = doc.select("ul.vcard-details > li.vcard-detail").first();
System.out.println(first.text());

Output:

Caldwell, Idaho, USA

Is there any way I can get the text

Yes: text() .

您可以使用以下代码

document.getElementsByClassName("vcard-detail")[0].innerHTML).split("</span>")[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM