PHPFan
PHPFan

Reputation: 796

Jsoup extract multi images sources

I work on this link to extract image's src

I tried this code and it extracted all images with their tags

Document doc = Jsoup.connect("http://deals.souq.com/sa-ar/?id_tag=48").get();
System.out.println(doc.select("div.padt-10 img")

Now I want to extract images with their src only, I tried with following code but It extracted the source of the first image only.

System.out.println(doc.select("div.padt-10 img").attr("src")

Upvotes: 0

Views: 133

Answers (1)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

The answer can be found in the source code. This doc.select("div.padt-10 img") returns Elements. The implementation of attr in Elements is this

public String attr(String attributeKey) {
    for (Element element : contents) {
        if (element.hasAttr(attributeKey))
            return element.attr(attributeKey);
    }
    return "";
}

As you can see, this will return immediately after the first valid match. If you want all the urls you can use this

Document doc = Jsoup.connect("http://deals.souq.com/sa-ar/?id_tag=48").get();
Elements imgs = doc.select("div.padt-10 img");
for(Element im : imgs)
    System.out.println(im.attr("src"));

Upvotes: 1

Related Questions