如何解析图像src使用JSOUP? [英] How to parse for image src using JSOUP?my

查看:257
本文介绍了如何解析图像src使用JSOUP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsoup来解析这个内容。

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>

我只想解析img src标签来获取图像url。

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

没有打印出来。

我如何解析这个?

>编辑:

这是我正在使用的代码。

Here is the code I am using.

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

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


推荐答案

这是错误的: p>

This is wrong:

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

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

编辑1

关于it did似乎工作...:这似乎对我来说很好。你如何测试这个?

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

这篇关于如何解析图像src使用JSOUP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆