簡體   English   中英

使用Jsoup解析html

[英]Using Jsoup to parse html

首先,我對使用Java編碼非常陌生,並且正在使用Android Studio。 我正在使用Jsoup轉到URL並獲取HTML源代碼。 我的代碼成功完成了此操作,現在我需要解析HTML的某一行。 我需要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作為字符串“ price”。 在最右邊縮進的HTML代碼行中。 誰能告訴我使用什么代碼來完成此任務?

在以下帶有注釋的源代碼中清楚地描述了所有內容。

@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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM