Extract image src using JSoup

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

I am trying to extract all the image url's from this webpage using jsoup? Can anyone offer help on how to do it? All the tags are formatted like this, but I only need the src image, not the ajaxsrc:

<IMG ajaxsrc="/pics32/160/MP/MPYXBXTSYVKAKJQ.20110918032436.jpg" src="http://image.cdnllnwnl.xosnetwork.com/pics32/160/MP/MPYXBXTSYVKAKJQ.20110918032436.jpg"> 

Here is the link: http://www.ncataggies.com/PhotoAlbum.dbml?DB_OEM_ID=24500&PALBID=417884

Is this the format?

        Document doc = null;     try {         doc = Jsoup.connect(articleLink).timeout(10000).get();      } catch (IOException ioe) {         return null;     }     Element content = doc.getElementById("div.thumb-image preview");     Elements links = content.getElementsByAttribute("IMG");     for (Element link : links) {       String source = link.attr("src");       Elements imageLinks = link.getElementsByAttribute(source);       for(Element imageLink: imageLinks){           //imageLink = picture link?       }  } 

That doesn't seem to be it. I have print statements in my code, and they aren't getting hit.

回答1:

You should be able to do something like this to get all img tags:

for (Element e : doc.select("img")) {     System.out.println(e.attr("src")); } 

This should select all img tags and then grab the src attribute and print to the console.



回答2:

Supposing you already have the Element according to this IMG, try this:

String source = img.attr("src");

This attr method is inherited from Node class

HTH



回答3:

Document document = Jsoup.connect(" http://www.ncataggies.com/PhotoAlbum.dbml?DB_OEM_ID=24500&PALBID=417884").get();          Elements elements =document.getElementsByTag("IMG");         for(int i=0;i<elements.size();i++)         {             System.out.println("Sources of "+ i +":"+elements.get(i).attr("src"));         } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!