获得使用Jsoup图片网址网址、图片、Jsoup

由网友(如今哥很缺爱)分享简介:我使用jSoup提取一个网站的HTML信息。但我在下面case.Html我有工作获取数据面临的一个问题包含一个部分,如下,我想所有这些图片的URL I am using jSoup to extract information from the html of a website. But I am facing a...

我使用jSoup提取一个网站的HTML信息。但我在下面case.Html我有工作获取数据面临的一个问题包含一个部分,如下,我想所有这些图片的URL

I am using jSoup to extract information from the html of a website. But I am facing a problem in fetching data in following case.Html I am working with contains one portion as below and I want to get all those image url

<ul class="myClass">
   <li>
      <a>
          <img src="myImageSrc1.png"/>
      </a>
   </li>

   <li>
      <a>
          <img src="myImageSrc2.png"/>
      </a>
   </li>

</ul>

我用的有点如下:

I am using somewhat as below:

doc = Jsoup.connect("http://www.myUrl").get();
castsImageUrl = doc.select("ul.cast > li > a > img");
for (Element el : castsImageUrl)System.out.println(el.text());

但我得到什么。我不能找出我的问题。任何人都可以帮我请

But I am getting nothing. I can not figure out my problem . Can anyone fix it for me please

推荐答案

根据关你提供,你可以通过调用 ATTR(字符串键)方法并传入的src (见文档)。例如:

Based off the exact HTML you provided, you can extract the image urls by calling the attr(String key) method and passing in src (see docs). Example:

    String html = "<ul class='myClass'><li><a><img src='myImageSrc1.png'/></a></li><li><a><img src='myImageSrc2.png'/></a></li></ul>";
    Document doc = Jsoup.parse(html);

    Elements castsImageUrl = doc.select("ul.myClass > li > a > img");
    for (Element el : castsImageUrl) System.out.println(el.attr("src"));

它输出:

11-06 09:45:11.313: I/System.out(454): myImageSrc1.png
11-06 09:45:11.313: I/System.out(454): myImageSrc2.png