开发者

How to parse for image src using JSOUP?my

开发者 https://www.devze.com 2023-04-05 23:18 出处:网络
I am trying to parse this content using jsoup. <div class=\"imageInlineCenter\" style=\"width: 468px;\" align=\"center\"><img src=\"http://xbox360media.ign.com/xbox360/image/article/117/1171

I am trying to parse this content using 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>

I only want to parse the img src tag to get the image url.

Here's what I am working with right now..

  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");
       开发者_如何学C                    }

Nothing is being printed out. Instead i am getting the message that it couldnt retrieve it.

How can i parse this?

EDIT:

Here is the code I am using.

It's not showing the catch message, or the 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");
                               }


This is wrong:

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

img is a tag not an attribute. src is an attribute of course.

Can't test it right now, but what about something like...

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

Edit 1
Regarding "it didn't seem to work...": it seemed to work fine for me. How are you testing this?

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

精彩评论

暂无评论...
验证码 换一张
取 消