Search code examples
javajsoup

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


I need get the currency data from website, here the website HTML table code:

<tr>
   <td class="currency-up"></td>
   <td class="currency">
      <a href="/dolar-kuru/" target="_blank" data-adservice-interactive-adunit="9927946/milliyet/uzmanpara/interstitial_oop">ABD Doları</a>
   </td>
   <td class>8,2805</td>
   <td class>8,2856</td>
</tr>

I wrote these code but I could not handle the code:

String url = "https://uzmanpara.milliyet.com.tr/doviz-kurlari/";
Document doc = null;
try {
    doc = Jsoup.connect(url).timeout(6000).get();
} catch (IOException ex) {
    Logger.getLogger(den3.class.getName()).log(Level.SEVERE, null, ex);
}
Element link = doc.select("href").first();

String linkHref = link.attr("href"); // "http://example.com/"
System.out.println(linkHref);

But I got this problem:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "org.jsoup.nodes.Element.attr(String)" because "link" is null

How can I handle this problem, how can I get currency rate.


Solution

  • You can try like this:

    Element link = doc.select("a[href]").first();
    

    If you just type href, it will search for the href tagname, but there is never such a tagname. You have to look for the href attribute of the a tag.

    Let's start with a simple example.

    Example, to get the value of the 2nd span below the element whose href value is /dolar-kuru/, you can try:

    // Example of selection with id.
    Element element2 = doc.select("#usd_header_son_data").first();
    String usd2 = element2.text();
    System.out.println(usd2);
    
    // Example of selecting 2nd span with href value and below. (1)
    Element element1 = doc.select("a[href='/dolar-kuru/'] > span > span").first();
    String usd1 = element1.text();
    System.out.println(usd1);
    
    // Example of selecting 2nd span with href value and below. (2)
    Element element3 = doc.select("a[href='/dolar-kuru/'] > span :nth-child(2)").first();
    String usd3 = element3.text();
    System.out.println(usd3);
    

    We can take the example one step further.

    Let's take both the buy and sell prices from a table of exchange rates.

    Elements elements = doc.select(".borsaMain > div:nth-child(2) > div:nth-child(1) > table td.currency");
    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(String.format("%s [buy=%s, sell=%s]",
                curreny.text(), buy.text(), sell.text()));
    }
    

    Will give an output that looks like this:

    ABD Doları [buy=8,2855, sell=8,2888]
    Euro [buy=9,8389, sell=9,8645]
    İngiliz Sterlini [buy=11,4203, sell=11,4775]
    Kanada Doları [buy=6,5696, sell=6,6091]
    İsviçre Frangı [buy=9,0128, sell=9,0671]
    Suudi Riyali [buy=2,2025, sell=2,2135]
    ...
    

    More different selectors can be used, see. https://jsoup.org/cookbook/extracting-data/selector-syntax