2015-02-05 264 views
0

theFirst off我对Java编码非常陌生,而且我正在使用Android Studio。我正在使用Jsoup转到一个URL并获取HTML源代码。我的代码成功地做到了这一点,现在我需要解析一个特定行的HTML。我需要从HTML中获得的字符串包含一个链接,但我不需要链接的地址,而只需显示为链接的字符串。这是我使用来完成这个类的代码:使用Jsoup来解析html

private class FetchAnton extends AsyncTask<Void, Void, Void> { 

    String price; 
    String url = "http://www.antoncoop.com/markets/cash.php"; 
    Elements hrefEles; 
    String value = null; 
    String html = null; 
    Document doc = null; 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 
      //Connect to website 
      html = Jsoup.connect(url).get().toString(); 

      if (html != null && html.length() > 0) { 
       doc = Jsoup.parse(html);   
       if (doc != null) { 
        /** Get all A tag element with HREF attribute like '/markets/cashchart.php?c=2246' **/ 
        hrefEles = doc.select("a[href*=/markets/cashchart.php?c=2246]"); 

        if (hrefEles != null && hrefEles.size() > 0) { 
         for (Element e: hrefEles) { 
          //value = e.ownText(); 
          // break; 
         } 

         price = value; 
        } 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

下面是部分HTML,我感兴趣的是:

</table> 
<br /> 
<table class="homepage_quoteboard" cellspacing="0" cellpadding="0" border="0" width="100%"> 
<thead> 
<tr class="section"> 
<td colspan="10">Wheat</td> 
</tr> 
<tr> 
<td width="10%">Name</td> 
<td width="10%">Delivery</td> 
<td width="10%">Delivery End</td> 
<td width="10%">Futures Month</td> 
<td width="10%" align="right">Futures Price</td> 
<td width="10%" align="right">Change</td> 
<td width="10%" align="right">Basis</td> 
<td width="10%" align="right">Cash Price</td> 
<td width="10%" align="right">Settlement</td> 
<td width="10%">Notes</td> 
</tr> 
</thead> 
<tbody> 
<script language="javascript">   
writeBidRow('Wheat',-60,false,false,false,0.5,'01/15/2015','02/26/2015','All','&nbsp;','&nbsp;',60,'even','c=2246&l=3519&d=G15',quotes['KEH15'], 0-0); 
writeBidRow('Wheat',-65,false,false,false,0.5,'07/01/2015','07/31/2015','All','&nbsp;','&nbsp;',60,'odd','c=2246&l=3519&d=N15',quotes['KEN15'], 0-0); 
</script> 
</tbody> 
</table> 

我唯一感兴趣的事情是获得$ 4.91作为名为“价格”的字符串。它是在HTML代码的最右边缩进的行中。任何人都可以告诉我用什么代码来实现这个目标?

+0

这个A标签除href外还有其他任何属性,例如类名或ID吗? –

+0

我相信班级名称是“偶数”。 – user3381831

回答

0

以下源代码中的所有内容都清晰地加以注释。

@Override 
protected Void doInBackground(Void... params) { 
    String value = null; 
    String html = null; 
    Document doc = null; 
    Elements hrefEles = null; 

    try { 
     //Connect to website 
     html = Jsoup.connect(url).get().toString(); 

     if (html != null && html.length() > 0) { 
      doc = Jsoup.parse(html); 

      if (doc != null) { 
       /** Get all A tag element with HREF attribute like '/markets/cashchart.php?c=2246' **/ 
       hrefEles = doc.select("a[href*=/markets/cashchart.php?c=2246]"); 

       if (hrefEles != null && hrefEles.size() > 0) { 
        for (Element e: hrefEles) { 
         value = e.ownText(); 
         break; 
        } 

        System.out.println("value: " + value); 
       } 
      } 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

我仍然遇到麻烦。我需要在私人课堂中实施这个。我将编辑我的帖子以显示整个班级的代码。我根据你在这里发布的内容改变了我的课程,但我还是得到了一个返回的空字符串。感谢您的帮助。 – user3381831

+0

我在阅读你的帮助之后修改了它之前添加了我的原始代码,因为我在修改它后感到非常困惑。我非常感谢你的帮助,不能够感谢你。 – user3381831

+0

我仍然有麻烦...我收到一个空白字符串返回。我在某一行停止执行代码,并将HTML文件显示在屏幕上。它看起来像手机屏幕上的html文件不同于我从浏览器中获得的html。我发布的html是从IE 11中的开发人员工具中的DOM Explorer获得的。看起来我将不得不继续尝试明天,但非常感谢您的帮助,我相信您的代码一旦找到我就会为我工作在html中的差异究竟发生了什么。我非常感谢你的帮助。 – user3381831