如何使用JSOUP解析图像src?我的

cod*_*e22 2 java android dom jsoup

我试图使用jsoup解析这个内容.

<div class="imageInlineCenter" style="width: 468px;" align="center"><img src="http://xbox360media.ign.com/xbox360/image/article/117/1171345/MW3_3_468_1306710207.jpg" align="middle" border="0" height="263" width="468"><div class="inlineImageCaption" style="width: 468px;">Your subwoofer will get a break during the stealthy start of the 'Mind the Gap' level, but only briefly.</div></div>
Run Code Online (Sandbox Code Playgroud)

我只想解析img src标签来获取图片网址.

这就是我现在正在使用的...

  try{
                  Elements img = jsDoc.select("div.imageInlineCenter");
                  String imgSrc = img.attr("img src");
                  System.out.println(imgSrc);



                 }
                 catch(Exception e){

                     Log.e("UPCOMING", "Couldnt retrieve the text");
                           }
Run Code Online (Sandbox Code Playgroud)

什么都没打印出来.相反,我得到它无法检索它的消息.

我怎么解析这个?

编辑:

这是我正在使用的代码.

它没有显示catch消息或system.out.

   try {
                 jsDoc = Jsoup.connect(url).get();

                  try{
                      Elements img = jsDoc.select("div.imageInlineCenter img[src]");
                      String imgSrc = img.attr("src");
                      System.out.println(imgSrc);





                     }
                     catch(Exception e){

                         Log.e("UPCOMING", "Couldnt retrieve the text");
                               }
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 6

这是错的:

String imgSrc = img.attr("img src");
Run Code Online (Sandbox Code Playgroud)

img是标签而不是属性.src当然是一个属性.

现在无法测试,但是像......那样的东西呢?

Elements img = jsDoc.select("div.imageInlineCenter img[src]");
String imgSrc = img.attr("src");
System.out.println(imgSrc);
Run Code Online (Sandbox Code Playgroud)

编辑1
关于"它似乎没有工作......":它似乎对我很好.你是如何测试的?

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Foo003 {
   private static final String TEST_URL_1 = "http://xbox360.ign.com/" +
        "articles/117/1171345p1.html";

   public static void main(String[] args) {
      Document jsDoc = null;

      try {
         jsDoc = Jsoup.connect(TEST_URL_1).get();
         // System.out.println(jsDoc);

         Elements img = jsDoc.select("div.imageInlineCenter img[src]");
         String imgSrc = img.attr("src");
         System.out.println(imgSrc);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)