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

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

问题描述

我试图用jsoup来解析这个内容。

>< D​​IV CLASS =imageInlineCenter的风格=WIDTH:468px; ALIGN =中心>< IMG SRC =htt​​p://xbox360media.ign.com/xbox360/image/article/117/1171345/MW3_3_468_1306710207.jpgALIGN =中间的边界=0HEIGHT = 263WIDTH =468>< D​​IV CLASS =inlineImageCaption的风格=WIDTH:468px;>在注意差距的水平,但只是暂时的隐身启动时你的低音炮将得到休息。 < / DIV>< / DIV>
!< ---高端形象格 - >

我只想解析IMG SRC标签来获取图像的URL。

下面就是我对工作现在..

  {尝试
                  元件IMG = jsDoc.select(div.imageInlineCenter);
                  字符串imgSrc = img.attr(IMG SRC);
                  的System.out.println(imgSrc);                 }
                 赶上(例外五){                     Log.e(即将到来的,不可能检索文本);
                           }

没有被打印出来。相反,我得到消息,它不可能恢复。

如何解析这个?

编辑:

下面是code我使用。

它不显示抓的消息,或System.out中。

  {尝试
                 jsDoc = Jsoup.connect(URL)获得();                  尝试{
                      ([来源] div.imageInlineCenter IMG)的元素IMG = jsDoc.select;
                      串imgSrc = img.attr(SRC);
                      的System.out.println(imgSrc);                     }
                     赶上(例外五){                         Log.e(即将到来的,不可能检索文本);
                               }


解决方案

这是错误的:

 字符串imgSrc = img.attr(IMG SRC);

IMG是的标签的不是属性。 SRC是当然的一个属性。

现在无法测试,但有关的东西是什么样子...

 元素IMG = jsDoc.select(div.imageInlineCenter IMG [来源]);
串imgSrc = img.attr(SRC);
的System.out.println(imgSrc);

修改1 结果
关于它似乎没有工作......:这似乎为我工作的罚款。你怎么测试呢?

 进口java.io.IOException异常;
进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;
进口org.jsoup.select.Elements;公共类Foo003 {
   私有静态最后弦乐TEST_URL_1 =htt​​p://xbox360.ign.com/+
        物品/ 117 / 1171345p1.html   公共静态无效的主要(字串[] args){
      文档jsDoc = NULL;      尝试{
         jsDoc = Jsoup.connect(TEST_URL_1)获得();
         //的System.out.println(jsDoc);         ([来源] div.imageInlineCenter IMG)的元素IMG = jsDoc.select;
         串imgSrc = img.attr(SRC);
         的System.out.println(imgSrc);      }赶上(IOException异常五){
         e.printStackTrace();
      }
   }
}

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

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.

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

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

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