0

Inside my Android app I want to receive some table data from an external website.

Lets say website page X has this table inside it's HTML:

<table summary="Foo" border="0" bgcolor="#ffffff" cellpadding="0"> </table>

How would I receive the strings inside all the cells of the second column of the table (top to bottom)?

So far what I have done is the following:

  1. Create an AsyncTask

  2. Use jSoup to scrape the external website.

I used the following code inside my AsyncTask:

ArrayList<String> list = new ArrayList<String>(); //table data
Document document = Jsoup.connect(url).get();
Elements nextTurns = document.select(":contains(Foo) td:eq(1)");            
        for (Element nextTurn : nextTurns) {
            list.add(nextTurn.text());
        }

When running the code it just seems to stop at the document.select statement and the GC is going crazy. After a very long time it does get past the document.select statement and it does get most of the data correct but it still has random other elements from the website.

I am pretty sure this is completely wrong:

Elements nextTurns = document.select(":contains(Foo) td:eq(1)"); 

But I am unsure how to fix it because the table also lacks any ID's. And I find this page confusing.

How can I fix the select statement and/or for loop so it fills up the ArrayList with data from the second table column?

Edit: by removing contains(Foo) it's now really fast so that's 1 problem less. I still need help with traversing the DOM elements to the second column of the table without taking a bunch of random parts of the website.

4

1 に答える 1

1

これはあなたの投稿に基づいて推測する正しい選択です

document.select("table[summary=Foo] tr");

上記のリストをループして、リストの<td>インデックス 1 にある秒を取得します。

于 2013-02-27T03:06:07.987 に答える