Java : Get FX sentiment from website example
A quick Java implementation of my previous Golang tutorial on how to "scrape" FX(forex) sentiment data from a web page using Jsoup. The Jsoup library provides web scraping part and all we need to do is to configure it to look for the right element to grab.
NOTE : The example below is configured to get sentiment data from dailyfx.com and it shouldn't be hard to re-configure to get sentiment data from other websites. Sentiment data can be useful for trader looking to see what the other market participants think or feel.
Here you go!
import java.util.Scanner;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class GetFXSentiment {
public static void main(String[] args) throws IOException {
// sanity check
if (args.length < 1) {
System.err.println("Usage : need url parameter to fetch sentiment");
System.exit(1);
}
String url = args[0];
try {
// Here we create a document object and use JSoup to fetch the website
Document doc = Jsoup.connect(url).get();
// With the document fetched, we use JSoup's title() method to fetch the title
System.out.printf("Title: %s\n", doc.title());
Elements row = doc.getElementsByClass("text-capitalize");
//Elements row = doc.select("ins-data text-capitalize"); -- not good
// we are interested in the first row, ignore the rest
String sentiment = row.get(0).text().toLowerCase();
if (sentiment.matches("mixed")) {
System.out.println(sentiment);
} else if (sentiment.matches("bearish")) {
System.out.println(sentiment);
} else if (sentiment.matches("bullish")) {
System.out.println(sentiment);
} else {
System.out.println("Missing sentiment data for this instrument/cross/pair.");
}
// In case of any IO errors, we want the messages written to the console
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sample output under Linux:
$ java -cp .:jsoup-1.12.1.jar GetFXSentiment http://dailyfx.com/eur-usd
Title: EUR/USD - Live Rate, Forecast, News and Analysis
mixed
Sample output under Windows:
java -cp .;jsoup-1.12.1.jar GetFXSentiment http://dailyfx.com/nzd-usd
Title: NZD/USD - New Zealand Dollar Price, Forecast, News & Analysis
bearish
References :
See also : Golang : Get FX sentiment from website example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+23.1k Golang : Get ASCII code from a key press(cross-platform) example
+26k Golang : Get executable name behind process ID example
+10.1k Golang : Convert file unix timestamp to UTC time example
+15.3k Golang : Validate hostname
+10.4k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+7.1k Golang : Calculate how many weeks left to go in a given year
+7.6k Golang : Getting Echo framework StartAutoTLS to work
+5.1k JavaScript/JQuery : Redirect page examples
+15.4k Golang : Get checkbox or extract multipart form data value example
+17.5k Golang : Qt image viewer example
+4.6k PHP : Extract part of a string starting from the middle
+29k Golang : Save map/struct to JSON or XML file