简体   繁体   English

使用Jsoup获取图像网址

[英]Getting Image Url using Jsoup

I am using jSoup to extract information from the html of a website. 我正在使用jSoup从网站的html中提取信息。 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 但是在以下情况下,我在获取数据时遇到了问题。我正在使用的HTML包含以下部分,我想获取所有这些图像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 谁能帮我修复一下

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 ). 根据您提供的确切HTML,可以通过调用attr(String key)方法并传入src来提取图像URL(请参阅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"));

Which outputs: 哪个输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM