【问题标题】:missing attribute缺少属性
【发布时间】:2013-01-21 02:25:13
【问题描述】:

好的,我使用 Jsoup 从这段代码中获取图像的链接

<div id="section_1" class="story  inview" bgimage="AAA" style="width: 1366px; height: 853.75px; background-image: url(http://Fake.link.comm);" scrollto="0">

我正在使用的代码是这样的

Document doc = Jsoup.connect(web).get();
Element content = doc.getElementById("section_1");
Elements elements = doc.getElementsByClass(content.className());
for( Element e : elements ) {
          String attr = e.attr("style");
          System.out.println( attr.substring( attr.indexOf("http://"), attr.indexOf(")") ) );
        }

但是,经过一番调查后,我意识到它给了我一个超出索引的错误,值为 -1 由于某种原因,解析器读取的代码是这样的

<div id="section_1" class="story" bgimage="AAA"> 

因此没有“样式”属性....有人可以告诉我为什么它会这样吗?非常感谢!

【问题讨论】:

    标签: java html jsoup


    【解决方案1】:

    如果您使用content 而不是e,它会起作用。

          String attr = content.attr("style");
          System.out.println( attr.substring( attr.indexOf("http://"), attr.indexOf(")") ) );
    

    您刚刚在e 中找到了其他div

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多