Daniele Asta
Daniele Asta

Reputation: 79

Jsoup parse a specific image in a html page

i would like parse a specific image from a html page.

<img id="icImg" class="img img500" itemprop="image" src="https://i.ebayimg.com/images/g/p4cAAOSwXSNann-Z/s-l500.jpg" style="" onload="picTimer=new Date().getTime();" clk="0" alt="Sigaretta-elettronica-liquid-pronto-per-l-039-uso-svapo-100-ml-GUSTI-A-SCELTA" mskuskip="false">

where "icImg" is the id i want to parse. is there a way to get the img with id icImg? because the .first method get me only the first image in the html page my code is:

public synchronized void getImg(){
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while((linkurl.isEmpty())){
                Thread.sleep(1000);
            }
            sem.acquire();
            Document doc = Jsoup.connect(linkurl).get();
            Element image = doc.select("img").first();
            String imgSrc = image.absUrl("src");
            InputStream in = new URL(imgSrc).openStream();
            bitmap = BitmapFactory.decodeStream(in);
            sem.release();

        }catch (IOException e){
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                immagine.setImageBitmap(bitmap);
            }
        });
    }


}).start();

Thanks for the help.

Upvotes: 2

Views: 767

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37414

You can pass id along with tag name using # as

Element image = doc.select("img#icImg").first();
// fetch all image elements tag with id, fetch the first element
String imgSrc = image.absUrl("src");

Upvotes: 3

Related Questions