Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...