Pregunta

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

¿Fue útil?

Solución

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

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top