java – Fetching values ​​from a website and using it

I’m working on a small project in order to improve my skill in Java. Its purpose is to exchange from USD to 3 other currencies. As you know rates are not stable so they need to be changed constantly, so I thought of data scraping using the Jsoup library, yet I’m facing an issue identifying this cell block name, td something since I’m scraping my data from a table, here is the code I’m using:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URL;
import java.io.IOException;

public class Scrap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final String url = "https://www.x-rates.com/table/?from=USD&amount=1";
        try {
            final Document doc = Jsoup.connect(url).get();
            for (Element row : doc.select("table.tablesorter ratesTable tbody")) {
                final String tip = row.select("td").text();
            }
        } catch (Exception ex) {
            System.out.println("Error fetching data");
            ex.printStackTrace();
        }
    }
}

and here is the HTML code I’m scraping from:

<table class="tablesorter ratesTable" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
        <th class="rtHeader rtHeaderCurr header headerSortDown">US Dollar<span class="ascAppend">?</span></th>
        <th class="rtHeader rtHeaderValues header">1.00 USD<span class="nonSortAppend">??</span></th>
        <th class="rtHeader rtHeaderValues header">inv. 1.00 USD<span class="nonSortAppend">??</span></th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td>Argentine Peso</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=ARS">126.761218</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=ARS&amp;to=USD">0.007889</a></td>
    </tr><tr>
        <td>Australian Dollar</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=AUD">1.462187</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=AUD&amp;to=USD">0.683907</a></td>
    </tr><tr>
        <td>Bahraini Dinar</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=BHD">0.376000</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=BHD&amp;to=USD">2.659574</a></td>
    </tr><tr>
        <td>Botswana Pula</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=BWP">12.461600</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=BWP&amp;to=USD">0.080247</a></td>
    </tr><tr>
        <td>Brazilian Real</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=BRL">5.306650</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=BRL&amp;to=USD">0.188443</a></td>
    </tr><tr>
        <td>British Pound</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=GBP">0.833368</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=GBP&amp;to=USD">1.199950</a></td>
    </tr><tr>
        <td>Bruneian Dollar</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=BND">1.400422</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=BND&amp;to=USD">0.714070</a></td>
    </tr><tr>
        <td>Bulgarian Lev</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=BGN">1.925554</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=BGN&amp;to=USD">0.519331</a></td>
    </tr><tr>
        <td>Canadian Dollar</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=CAD">1.298121</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=CAD&amp;to=USD">0.770344</a></td>
    </tr><tr>
        <td>Chilean Peso</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=CLP">955.448409</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=CLP&amp;to=USD">0.001047</a></td>
    </tr><tr>
        <td>Chinese Yuan Renminbi</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=CNY">6.698005</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=CNY&amp;to=USD">0.149298</a></td>
    </tr><tr>
        <td>Colombian Peso</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=COP">4404.258750</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=COP&amp;to=USD">0.000227</a></td>
    </tr><tr>
        <td>Croatian Kuna</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=HRK">7.402757</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=HRK&amp;to=USD">0.135085</a></td>
    </tr><tr>
        <td>Czech Koruna</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=CZK">24.230525</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=CZK&amp;to=USD">0.041270</a></td>
    </tr><tr>
        <td>Danish Krone</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=DKK">7.327581</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=DKK&amp;to=USD">0.136471</a></td>
    </tr><tr>
        <td>Emirati Dirham</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=AED">3.672500</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=AED&amp;to=USD">0.272294</a></td>
    </tr><tr>
        <td>Euro</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=EUR">0.984520</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=EUR&amp;to=USD">1.015723</a></td>
    </tr><tr>
        <td>Hong Kong Dollar</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=HKD">7.848745</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=HKD&amp;to=USD">0.127409</a></td>
    </tr><tr>
        <td>Hungarian Forint</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=HUF">395.424037</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=HUF&amp;to=USD">0.002529</a></td>
    </tr><tr>
        <td>Icelandic Krona</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=ISK">137.358554</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=ISK&amp;to=USD">0.007280</a></td>
    </tr><tr>
        <td>Indian Rupee</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=INR">79.335352</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=INR&amp;to=USD">0.012605</a></td>
    </tr><tr>
        <td>Indonesian Rupiah</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=IDR">14966.909349</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=IDR&amp;to=USD">0.000067</a></td>
    </tr><tr>
        <td>Iranian Rial</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=IRR">42289.358413</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=IRR&amp;to=USD">0.000024</a></td>
    </tr><tr>
        <td>Israeli Shekel</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=ILS">3.481857</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=ILS&amp;to=USD">0.287203</a></td>
    </tr><tr>
        <td>Japanese Yen</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=JPY">136.392411</a></td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=JPY&amp;to=USD">0.007332</a></td>
    </tr><tr>
        <td>Kazakhstani Tenge</td>
        <td class="rtRates"><a href="https://www.x-rates.com/graph/?from=USD&amp;to=KZT">473.313146</a></td>
        <td class="rtRates"><a

When I try to do a test run, nothing shows up not even an error, so where is my mistake? Or what am I missing? (KEEP IN MIND THIS IS ONLY THE CLASS THAT FETCHES THE DATA) My plan is to fetch the rates and write them to a text file, then read them from there.

Leave a Comment