Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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" 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

EDIT :

Element elements = Jsoup.parse(s).select(".item").first();
    System.out.println(elements.attr("data-youtube_code"));
    Element element1 = elements.select(".image img").first();
    System.out.println(element1.attr("data-src"));

Output:

code_for_youtuber
http://url_of_image.jpg

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...