简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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