Edward van Raak Edward van Raak - 25 days ago 8
HTML Question

Receive table data from external website in Android using jSoup

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.

Answer

This is the correct selection, guessing based on your post

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

Loop through the list above, and get the second <td> which is at index 1 of the list.