2015-02-05 162 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