Pergunta

I am trying to parse the html but getting nullpointor. I want to extract image uri from the below html.

String html = "<div class=\"thumb-box thumb-160\"><a class=\"mimg\" data-id=\"1394085169856_6744\" href=\"#\"><img class=\"thumb\" src=\"http://i.ytimg.com/vi/u7deClndzQw/hqdefault.jpg\" style=\"top: -15px;\"><span class=\"btn\"></span></a></div>";

Document document = Jsoup.parse(html);

Element element = document.select("div.thumb-box thumb-160").first();

System.out.println(element.select("img").attr("src"));
Foi útil?

Solução

Element element = document.select("div.thumb-box thumb-160").first();

you have to use . (dot) for every class

Element element = document.select("div.thumb-box.thumb-160").first();

Besides it is rather straight forward do select like this

Element element = document.select("div.thumb-box.thumb-160:eq(0) a").first(); 

This yould yet you anchor element out of the box

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top