簡體   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