繁体   English   中英

如何使用JSOUP解析图像src?

[英]How to parse for image src using JSOUP?my

我正在尝试使用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>

我只想解析img src标签以获取图像URL。

这就是我现在正在处理的内容。

  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");
                           }

什么都没有打印出来。 相反,我收到的消息是它无法检索它。

我该如何解析?

编辑:

这是我正在使用的代码。

它没有显示捕获消息或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");
                               }

这是错误的:

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

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

现在无法测试,但是类似...

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

编辑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();
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM