From 069608a7b26e3aacb972958ed765daca6e195bff Mon Sep 17 00:00:00 2001 From: Nithu Lakshmi Date: Sun, 15 Dec 2024 14:36:18 +0530 Subject: [PATCH 1/2] Commit Done --- CurrencyExchangeRateTable/.classpath | 11 ++ CurrencyExchangeRateTable/.project | 17 +++ .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 14 ++ .../Currency_Rate_Table.class | Bin 0 -> 5771 bytes .../table_extraction/Currency_Rate_Table.java | 128 ++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 CurrencyExchangeRateTable/.classpath create mode 100644 CurrencyExchangeRateTable/.project create mode 100644 CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs create mode 100644 CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs create mode 100644 CurrencyExchangeRateTable/bin/table_extraction/Currency_Rate_Table.class create mode 100644 CurrencyExchangeRateTable/src/table_extraction/Currency_Rate_Table.java diff --git a/CurrencyExchangeRateTable/.classpath b/CurrencyExchangeRateTable/.classpath new file mode 100644 index 0000000..900895c --- /dev/null +++ b/CurrencyExchangeRateTable/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/CurrencyExchangeRateTable/.project b/CurrencyExchangeRateTable/.project new file mode 100644 index 0000000..959c8c5 --- /dev/null +++ b/CurrencyExchangeRateTable/.project @@ -0,0 +1,17 @@ + + + CurrencyExchangeRateTable + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs b/CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/CurrencyExchangeRateTable/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs b/CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6e5f874 --- /dev/null +++ b/CurrencyExchangeRateTable/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=22 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=22 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=22 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 0000000000000000000000000000000000000000..387177650be384eb4b268f79daae4b850b79f609 GIT binary patch literal 5771 zcmcIod3;pW75?sQZ!$c#F#-XUs4Q7XhC~gTuxJ(_7>F=2F;c+CDPVfv-*vUz{<8WiKT(-Bs9YmSWvdBZ91l?l{U=2hVo7Y^95EV(|wU- zN;j|5A{jldn?^PfiS?(G2|dzeUz*d#fSwLv9*Sn67z-4fEpSdDvo3v~Gg-w#EaI*c znyK~a0_A1pZKpI?-ky?>W*TN;F_tP=BCz6&3{;$hbGg!>VfG{k1|i5N&X3iP>xCk z70j`n6w4sg&Dpdj5m2!jRRTrH-l!f+nmx?+!m_R9PWtj@HiWPo5y_u50(E7l#cOGssPEN~Syd^{X;^S^@dEvJhZfSuL4Z+DKU~LwYQ0CDTEyCuyCOR&yvBk2emN ztyx`n@tX2ksK-VXm*H}O*}83@M{R3?OA7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DD?qYBk?WAS-V>ryB8;kV5dh zin$6-h$05Yf5~zKyM|1b$Benf_}v(OKZ4*D6{Gm;QTt_pQ(tyVCCpt(q6gS_LzI;{ x{PdkXgoHSDH(uemT#EJGs^DdUfWPz6LjwGh*D&uEv$OpxP{cYb#=iqa{|5)hxT637 literal 0 HcmV?d00001 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(); + } + } +} From 162596278ff937adc571d06c22ca0459d330926a Mon Sep 17 00:00:00 2001 From: Nithu Lakshmi <110821617+niths09@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:36:44 +0530 Subject: [PATCH 2/2] Delete WikiPopulationScraper directory --- WikiPopulationScraper/.classpath | 11 --- WikiPopulationScraper/.project | 17 ---- .../org.eclipse.core.resources.prefs | 2 - .../.settings/org.eclipse.jdt.core.prefs | 14 ---- .../WebTableDataExtraction.class | Bin 4346 -> 0 bytes .../WebTableDataExtraction.java | 74 ------------------ 6 files changed, 118 deletions(-) delete mode 100644 WikiPopulationScraper/.classpath delete mode 100644 WikiPopulationScraper/.project delete mode 100644 WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs delete mode 100644 WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs delete mode 100644 WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class delete mode 100644 WikiPopulationScraper/src/data_extraction/WebTableDataExtraction.java diff --git a/WikiPopulationScraper/.classpath b/WikiPopulationScraper/.classpath deleted file mode 100644 index 900895c..0000000 --- a/WikiPopulationScraper/.classpath +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/WikiPopulationScraper/.project b/WikiPopulationScraper/.project deleted file mode 100644 index 68201ba..0000000 --- a/WikiPopulationScraper/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - WikiPopulationScraper - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs b/WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 99f26c0..0000000 --- a/WikiPopulationScraper/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/=UTF-8 diff --git a/WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs b/WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 6e5f874..0000000 --- a/WikiPopulationScraper/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,14 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=22 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=22 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=22 diff --git a/WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class b/WikiPopulationScraper/bin/data_extraction/WebTableDataExtraction.class deleted file mode 100644 index 1811f0ffd52968e21c425ba7ee677544878765f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4346 zcmb_g`&$&(8Ga8eJIgYFK~UEivq?3A0;`zV1T;nv3Zm|eptjGnx2KA1Hf)RZ+$TVO-uslH{a20hobCpwyjHQXhz!*M54PEOj> zMk+5YX`6*?Dn02sS()ncUx!`utaRg8hsr9{V}phUfqTN1(JO(lj*ZyFb7u|Pn2-WZ zjZJ-PF4%S~r~I1lsKRD!(QvoG_7Ai`$356eO*5vQac1b8J8By7C8;Qh`!(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P)B^#& ztJ$kW4Qe^U#pRWJt^+gb2G*Ut#suB9i1h-CNT|oX0W15ErQfCkB_%kCSs9Q zgYiVHV%1<(BDSPtX}O+=X{#iw6EP}%pQ0N2AW@l!C#n*9qB>D?9p^v`nqP>DhG>H7 zXyi+`AHIw&{mt`ufgb$4z-838*9Of*?OQ?P3PZC=bc$|V;QOc;WH2Iru+H*#@S9*S8bv3+0D7=r9K}IuJ|Ir;eTtR((|l)e$Op>H0T}QlyhwXu z;xxXDuMjFJu^wN=*J%GiQI4-uUpejg7ru#a@dVTOC%%pE5R)GMj*Iv%!FPc)_&v^Q zl>ITj&)FEQJBJ_O5&?J%Q?&OI&;Blk@gugXXzw9PUV=`moADA`)wF#hUS_L?mPhat zyh00pO3OCz9cX=(S_2KQQp07=?Spokj?pyyOhdhfHVsEK7#gxni2vY5*=^##ESLp# zUFg^F^LPcV;>Sdg7I=#LtMsX@$1fM~tILQz!6bP7ia!{o=v6vEWmz4~0=l`dhy5em zxpyh!;=alAoL6S?{}sI&u12`<8oMfW7Ws~G%eVNuiMQF?%-KKU&-g22znS&Lt!O2& KP>;VwEB^-~xW|P6 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(); - } - } -}