diff --git a/WikiPopulationScraper/.classpath b/CurrencyExchangeRateTable/.classpath similarity index 100% rename from WikiPopulationScraper/.classpath rename to CurrencyExchangeRateTable/.classpath diff --git a/WikiPopulationScraper/.project b/CurrencyExchangeRateTable/.project similarity index 89% rename from WikiPopulationScraper/.project rename to CurrencyExchangeRateTable/.project index 68201ba..959c8c5 100644 --- a/WikiPopulationScraper/.project +++ b/CurrencyExchangeRateTable/.project @@ -1,6 +1,6 @@ - WikiPopulationScraper + CurrencyExchangeRateTable diff --git a/WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs b/CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs similarity index 100% rename from WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs rename to CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs diff --git a/WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs b/CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs rename to CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs diff --git a/CurrencyExchangeRateTable/bin/table_extraction/Currency_Rate_Table.class b/CurrencyExchangeRateTable/bin/table_extraction/Currency_Rate_Table.class new file mode 100644 index 0000000..3871776 Binary files /dev/null and b/CurrencyExchangeRateTable/bin/table_extraction/Currency_Rate_Table.class differ diff --git a/CurrencyExchangeRateTable/src/table_extraction/Currency_Rate_Table.java b/CurrencyExchangeRateTable/src/table_extraction/Currency_Rate_Table.java new file mode 100644 index 0000000..8748bf0 --- /dev/null +++ b/CurrencyExchangeRateTable/src/table_extraction/Currency_Rate_Table.java @@ -0,0 +1,128 @@ +package table_extraction; + +import java.time.Duration; +import java.util.List; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +public class Currency_Rate_Table { + + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + driver.manage().window().maximize(); + + try { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + driver.get("https://www.xe.com/currencytables/"); + waitForTheUser(); + + JavascriptExecutor scrollDownOne = (JavascriptExecutor) driver; + scrollDownOne.executeScript("window.scrollBy(0,650)"); + waitForTheUser(); + + // Extract data from the first table + System.out.println("Live Currency Rates"); + printBoxHeader(new String[]{"Currency", "Rate", "Change"}); // Boxed Header for 3 columns + WebElement liveCurrencyTable = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//table[@class='sc-621fdd77-0 fFOksQ'])[1]"))); + waitForTheUser(); + extractLiveCurrencyData(liveCurrencyTable); + + // Extract data from the second table + System.out.println("\nCentral Bank Rates"); + printBoxHeader(new String[]{"Currency", "Interest Rate"}); // Boxed Header for 2 columns + WebElement centralBankTable = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//table[@class='sc-621fdd77-0 fFOksQ'])[2]"))); + waitForTheUser(); + extractCentralBankData(centralBankTable); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + driver.quit(); + } + } + + public static void extractLiveCurrencyData(WebElement table) { + List rows = table.findElements(By.tagName("tr")); + for (int i = 1; i < rows.size(); i++) { + WebElement row = rows.get(i); + List cols = row.findElements(By.tagName("td")); + if (cols.size() > 1) { + String currency = cols.get(0).getText().trim(); // Currency (1st column) + String rate = cols.get(1).getText().trim(); // Rate (2nd column) + String change = cols.get(2).getText().trim(); // Change (3rd column) + printRow(new String[]{currency, rate, change}, 3); // Specify 3 columns + } + } + printBottomLine(3); // Add bottom line to close the table (3 columns) + } + + public static void extractCentralBankData(WebElement table) { + List rows = table.findElements(By.tagName("tr")); + for (int i = 1; i < rows.size(); i++) { + WebElement row = rows.get(i); + List cols = row.findElements(By.tagName("td")); + if (cols.size() > 1) { + String currency = cols.get(0).getText().trim(); // Currency (1st column) + String interestRate = cols.get(1).getText().trim(); // Interest Rate (2nd column) + printRow(new String[]{currency, interestRate}, 2); // Specify 2 columns + } + } + printBottomLine(2); // Add bottom line to close the table (2 columns) + } + + // Prints a row with lines separating columns dynamically based on column count + public static void printRow(String[] columns, int columnCount) { + if (columnCount == 3) { + System.out.printf("| %-15s | %-15s | %-15s |%n", + (columns.length > 0 ? columns[0] : ""), + (columns.length > 1 ? columns[1] : ""), + (columns.length > 2 ? columns[2] : "")); + } else if (columnCount == 2) { + System.out.printf("| %-15s | %-15s |%n", + (columns.length > 0 ? columns[0] : ""), + (columns.length > 1 ? columns[1] : "")); + } + } + + // Prints a header with a box-style format dynamically based on column count + public static void printBoxHeader(String[] headers) { + if (headers.length == 3) { + String line = "+-----------------+-----------------+-----------------+"; + System.out.println(line); // Top border + System.out.printf("| %-15s | %-15s | %-15s |%n", + headers[0], + headers[1], + headers[2]); + System.out.println(line); // Bottom border + } else if (headers.length == 2) { + String line = "+-----------------+-----------------+"; + System.out.println(line); // Top border + System.out.printf("| %-15s | %-15s |%n", + headers[0], + headers[1]); + System.out.println(line); // Bottom border + } + } + + // Prints a bottom line to close the table dynamically based on column count + public static void printBottomLine(int columnCount) { + StringBuilder line = new StringBuilder("+"); + for (int i = 0; i < columnCount; i++) { + line.append("-----------------+"); + } + System.out.println(line.toString()); + } + + public static void waitForTheUser() { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class b/WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class deleted file mode 100644 index 1811f0f..0000000 Binary files a/WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class and /dev/null differ diff --git a/WikiPopulationScraper/src/data_extraction/WebTableDataExtraction.java b/WikiPopulationScraper/src/data_extraction/WebTableDataExtraction.java deleted file mode 100644 index dbb0edc..0000000 --- a/WikiPopulationScraper/src/data_extraction/WebTableDataExtraction.java +++ /dev/null @@ -1,74 +0,0 @@ -package data_extraction; - -import java.time.Duration; -import java.util.List; -import org.openqa.selenium.By; -import org.openqa.selenium.JavascriptExecutor; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; - -public class WebTableDataExtraction { - - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - driver.manage().window().maximize(); - - try { - // Print the message before extraction - System.out.println("List of countries and territories by total population"); - - // Open the Wikipedia page - WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - driver.get("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population"); - waitForTheUser(); - - JavascriptExecutor scrollDownOne = (JavascriptExecutor) driver; - scrollDownOne.executeScript("window.scrollBy(0,1200)"); - waitForTheUser(); - - // Locate the table by XPath - WebElement table = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='wikitable sortable sticky-header sort-under mw-datatable col2left col6left jquery-tablesorter']"))); - waitForTheUser(); - - // Get all rows from the table (skip the first row as it's the header) - List rows = table.findElements(By.tagName("tr")); - - // Loop through each row - for (int i = 1; i < rows.size(); i++) { // Start from 1 to skip header - WebElement row = rows.get(i); - - // Get all columns (td) in the current row - List cols = row.findElements(By.tagName("td")); - - // Extract the Location, Population, % of world, Date, Source and Notes - if (cols.size() > 1) { - String location = cols.get(0).getText().trim(); // Location (1st column) - String population = cols.get(1).getText().trim(); // Population (2nd column) - String perc_world = cols.get(2).getText().trim(); // % of world (3rd column) - String date = cols.get(3).getText().trim(); // Date (4th column) - String source = cols.get(4).getText().trim(); // Source (5th column) - String notes = cols.get(5).getText().trim(); // Notes (6th column) - - // Print the extracted data - System.out.println("Location: " + location + " | Population: " + population + " | % of world: " + perc_world + " | Date: " + date + " | Source: " + source + " | Notes: " + notes); - } - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - // Close the browser after scraping - driver.quit(); - } - } - - public static void waitForTheUser() { - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } -}