gpt4 book ai didi

java - Jsoup解析html页面中的特定图片

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:19 24 4
gpt4 key购买 nike

我想从 html 页面解析特定图像。

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

其中“icImg”是我要解析的 ID。有没有办法获取 id 为 icImg 的 img?因为 .first 方法只让我得到 html 页面中的第一张图片我的代码是:

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

感谢您的帮助。

最佳答案

您可以使用 # as

将 id 与标签名称一起传递
Element image = doc.select("img#icImg").first();
// fetch all image elements tag with id, fetch the first element
String imgSrc = image.absUrl("src");

关于java - Jsoup解析html页面中的特定图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com