ben ben - 3 years ago 95
HTML Question

How can I retrieve data from html using Jsoup

I'm new to HTML and I'm trying to learn a little about the HTML tags by trying to retrieve data from an HTML String.

<li>
<div class="item" data-youtube_code="code_for_youtuber" data-feature_code="data" data-feature_url="/movies/Truman">
<div class="title">
<span>the title of the video</span>
</div>
<div class="image">
<img src="/media/image.png" data-src="http://url_of_image.jpg" alt="">
</div>
</div> </li>


I'm using the Java Jsoup library and so far I've manage to extract the
<span>
content using:

Document doc = Jsoup.connect("http://www.yesplanet.co.il/movies").get();
System.out.println(doc.html());
Elements elem = doc.select(".item").text();


How can I get other things such as the
data-youtube_code
and the
img src
.

Edit:
For example:

System.out.println("doc...data-youtube_code");//some code that retrieves
//data-youtube_code. The ouptup will be "code_for_youtuber"

System.out.println("data-src")
//some code that retrieves
//data-src. The ouptup will be "http://url_of_image.jpg"

Answer Source

You can simply select first div and get the value by attribute

    Element elements = Jsoup.parse(s).select("div").first();
    System.out.println(elements.attr("data-youtube_code"));

Output:

code_for_youtuber

Since you are beginner i suggest you to look for this link

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download