如何使用Jsoup从html检索数据 [英] How can I retrieve data from html using Jsoup

查看:92
本文介绍了如何使用Jsoup从html检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是HTML的新手,我试图通过尝试从HTML字符串中检索数据来学习一些有关HTML标签的信息.

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> 

我正在使用Java Jsoup库,到目前为止,我已经设法使用以下方法提取< span> 内容:

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();        

如何获取其他内容,例如 data-youtube_code img src .

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

例如:

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" 

推荐答案

您只需选择第一个div并按属性获取值

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"));

输出:

code_for_youtuber

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"));

输出:

code_for_youtuber
http://url_of_image.jpg

由于您是初学者,我建议您寻找此链接

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

这篇关于如何使用Jsoup从html检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆