Skip to content
Advertisement

Parsing currency exchange data from https://uzmanpara.milliyet.com.tr/doviz-kurlari/

I prepare the program and I wrote this code with helping but the first 10 times it works then it gives me NULL values,

String url = "https://uzmanpara.milliyet.com.tr/doviz-kurlari/";
//Document doc = Jsoup.parse(url);
Document doc = null;
try {
    doc = Jsoup.connect(url).timeout(6000).get();
} catch (IOException ex) {
    Logger.getLogger(den3.class.getName()).log(Level.SEVERE, null, ex);
}
int i = 0;
String[] currencyStr = new String[11];
String[] buyStr = new String[11];
String[] sellStr = new String[11];
Elements elements = doc.select(".borsaMain > div:nth-child(2) > div:nth-child(1) > table.table-markets");
for (Element element : elements) {
    Elements curreny = element.parent().select("td:nth-child(2)");
    Elements buy = element.parent().select("td:nth-child(3)");
    Elements sell = element.parent().select("td:nth-child(4)");
    System.out.println(i);
    currencyStr[i] = curreny.text();
    buyStr[i] = buy.text();
    sellStr[i] = sell.text();
    System.out.println(String.format("%s [buy=%s, sell=%s]",
            curreny.text(), buy.text(), sell.text()));
    i++;
}

for(i = 0; i < 11; i++){
    System.out.println("currency: " + currencyStr[i]);
    System.out.println("buy: " + buyStr[i]);
    System.out.println("sell: " + sellStr[i]);
}

here is the code, I guess it is a connection problem but I could not solve it I use Netbeans, Do I have to change the connection properties of Netbeans or should I have to add something more in the code

can you help me?

Advertisement

Answer

There’s nothing wrong with the connection. Your query simply doesn’t match the page structure.

Somewhere on your page, there’s an element with class borsaMain, that has a direct child with class detL. And then somewhere in the descendants tree of detL, there is your table. You can write this as the following CSS element selector query:

.borsaMain > .detL table

There will be two tables in the result, but I suspect you are looking for the first one.

So basically, you want something like:

Element table = doc.selectFirst(".borsaMain > .detL table");
for (Element row : table.select("tr:has(td)")) {
    // your existing loop code
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement