Using JSOUP to get the first valid image in a HTML String
This is my code but sometimes it fails to get a valid image for some HTML
String.
If I log every images variable in the loop I can see that a valid image
exists but my code use another one sometimes not valid or the function
return a null image.
what's wrong? thanks
private String getFirstImage(String htmlString){
if(htmlString==null) return null;
String img ="";
Document doc = Jsoup.parse(htmlString);
Elements imgs = doc.getElementsByTag("img");
for (Element imageElement : imgs) {
if(imageElement!=null){
//for each element get the srs url
img = imageElement.absUrl("src");
if( !img.contains("doubleclick.net") &&
!img.contains("feedburner.com") &&
!img.contains("feedsportal.com") &&
!img.contains("ads"))
return img;
}
}
return null;
}
No comments:
Post a Comment