DKM
DKM

Reputation: 270

Get IMG's source using JSOUP parser

Using JSOUP I'd like to get link to for example photo from IMG tag. Sometimes ELEMENT doesn't have IMG tag inside HTML code. "Element link" has extracted HTML code from source downloaded code. To do this I've created method:

private String getPhotoForCover(Element link) throws NullPointerException
    {
        String photoLink = null;


        Log.i("IMG",link.getElementById("img").attr("src").toString());

// returning here "null" because I for now want to see extracted link in LOG.
        return photoLink;

    }

For example, HTML code from "Element link" is like following (shorted version):

<!-- <li> <a href="/pl/consignment/show/776609"><img src="/var/images/community_gallery/42017/595121/thumbnail.jpeg" class="thumbnail"></a> 
                                                                     </li> -->

After trying to get "src" content I get every time NULLPOINTER. How to properly get this "IMG" tag and it's "SRC" content?

Upvotes: 0

Views: 50

Answers (1)

A. Steenbergen
A. Steenbergen

Reputation: 3440

Repalce link.getElementById("img") with link.select("img")

Why?

getElementById("img") looks for an element with the id "img", e.g. <div id="img">, this is not what you need. To find image elements you have to use select("img")

Also, you might want to get the absolute path instead of the relative path by using attr("abs:src") instead of attr("src")

Upvotes: 1

Related Questions