user3650602 user3650602 - 5 months ago 6
HTML Question

JSoup - Extracting table data error

I've just began a small project of which I will need to gather historical data on global currency pairs. Following a answer from this question Extract Data out of table with JSoup, I have the code pasted below.

However I keep receiving an

IndexOutOfBoundException
, although the 'data' Elements array is of size 7?

I've been scratching my head for almost an hour, and would appreciate if someone could point out where I have gone wrong!

Main Class

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class MainClass {


public static void main(String[] args) throws IOException{

Document doc = Jsoup.connect("http://www.myfxbook.com/forex-market/currencies/GBPUSD-historical-data").get();

Element table = doc.getElementById("symbolMarket");

List<Entry> entries = new ArrayList<Entry>();

for(Element row : table.select("tr")){

int index = 0;
Entry tableEntry = new Entry();
Elements data = row.select("td");

tableEntry.setDate(data.get(index++).text());
tableEntry.setOpen(data.get(index++).text());
tableEntry.setHigh(data.get(index++).text());
tableEntry.setLow(data.get(index++).text());
tableEntry.setClose(data.get(index++).text());
tableEntry.setChangePips(data.get(index++).text());
tableEntry.setChangePercent(data.get(index++).text());

entries.add(tableEntry);

}

}

}


Entries Class

public class Entry {

private String date;
private String open;
private String high;
private String low;
private String close;
private String changePips;
private String changePercent;

public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getOpen() {
return open;
}
public void setOpen(String open) {
this.open = open;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getClose() {
return close;
}
public void setClose(String close) {
this.close = close;
}
public String getChangePips() {
return changePips;
}
public void setChangePips(String changePips) {
this.changePips = changePips;
}
public String getChangePercent() {
return changePercent;
}
public void setChangePercent(String changePercent) {
this.changePercent = changePercent;
}



}

Answer

You are trying to get the data from the table header... you have to skip it.

public static void main(String[] args) throws IOException {
        Document doc = Jsoup.connect("http://www.myfxbook.com/forex-market/currencies/GBPUSD-historical-data").get();

        Element table = doc.getElementById("symbolMarket");

        List<Entry> entries = new ArrayList<Entry>();

        Elements elements = table.select("tr");
        Iterator<Element> itr = elements.iterator();
        itr.next(); //skip header data

        while ( itr.hasNext() ) {
            int index = 0;
            Entry tableEntry = new Entry();
            Elements data = itr.next().select("td");

            tableEntry.setDate(data.get(index++).text());
            tableEntry.setOpen(data.get(index++).text());
            tableEntry.setHigh(data.get(index++).text());
            tableEntry.setLow(data.get(index++).text());
            tableEntry.setClose(data.get(index++).text());
            tableEntry.setChangePips(data.get(index++).text());
            tableEntry.setChangePercent(data.get(index++).text());
            entries.add(tableEntry);

        }       




    }