如何使用jsoup解析表中的值?

时间:2014-04-02 21:45:28

标签: java android jsoup

<table class="local_table nbu_rate">
    <tr>
        <th>Currency</th>
    <th class="align_left">Name</th>
        <th class="width_25">Course</th>
    </tr>
    <tr>
        <td>USD</td>
        <td class="align_left">USD</td>
        <td>11.2416</td>
    </tr>
    <tr>
        <td>EUR</td>
        <td class="align_left">Euro</td>
        <td>15.5078</td>
    </tr>
    </table>

如何从表中解析值。比如11.24? 我试过这样做:

        try {
            doc = Jsoup.parse(url, 3000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Element table = doc.select("table[class=local_table nbu_rate]").first();

        Iterator<Element> ite = table.select("td[USD]").iterator();

        System.out.println("Value 1: " + ite.next().text());

但是我的程序崩溃了。 经度:

04-02 21:38:29.764: E/AndroidRuntime(5769): FATAL EXCEPTION: AsyncTask #1
04-02 21:38:29.764: E/AndroidRuntime(5769): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.lang.Thread.run(Thread.java:856)
04-02 21:38:29.764: E/AndroidRuntime(5769): Caused by: java.util.NoSuchElementException
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:572)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at com.example.parsedatatest.MainActivity$NewThread.doInBackground(MainActivity.java:92)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at com.example.parsedatatest.MainActivity$NewThread.doInBackground(MainActivity.java:1)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-02 21:38:29.764: E/AndroidRuntime(5769):     ... 4 more

1 个答案:

答案 0 :(得分:1)

问题在于

Element table = doc.select("table[class=local_table nbu_rate]").first();

请注意,类名的css选择器包含点.。你的选择器应该是

Element table = doc.select("table.local_table.nbu_rate").first();

或者,只有一个班级应该这样做:

Element table = doc.select("table.local_table").first();

编辑:

你的第二个问题似乎是

Iterator<Element> ite = table.select("td[USD]").iterator();

css选择器"td[USD]"将查找名为USD的属性<td>元素。这不存在。您需要调整选择器。 USD是内部HTML,而不是属性。

我想您需要阅读第3个<td>来获取数字值。但是,为了有意义,你可能也需要知道货币......