I am in the process of designing a program, purely out of personal interest and for personal use. I am attempting to extract a stock price for a stock i.e "GOOG" from https://finance.yahoo.com/.
I stumbled across a Java class supplied by Princeton that seem(ed) to do exactly the job I'm looking for. However, I am aware that the website changes regularly including the HTML source code.
The HTML source has probably changed since the writing of the program and I am just looking for some assistance in finding where exactly to extract the share price from the HTML code. Princeton's implementation uses the following approach:
// Given symbol, get current stock price.
public static double priceOf(String symbol) {
String html = readHTML(symbol);
int p = html.indexOf("price.0", 0); // "price.0" index
int from = html.indexOf(">", p); // ">" index
int to = html.indexOf("</span>", from); // "</span>" index
String price = html.substring(from + 1, to);
// remove any comma separators
return Double.parseDouble(price.replaceAll(",", ""));
}
More than likely, the part that is throwing it off at the moment is the delimiting lines:
int p = html.indexOf("price.0", 0); // "price.0" index
int from = html.indexOf(">", p); // ">" index
int to = html.indexOf("</span>", from); // "</span>" index
String price = html.substring(from + 1, to);
I have scoured the HTML source code tirelessly and can't seem to find an accurate location to delimit from -> to and would greatly appreciate any assistance provided.
Current Java error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
at StockQuote.priceOf(StockQuote.java:52)
at Holding.<init>(Holding.java:24)
at PortfolioTracker.main(PortfolioTracker.java:6)
Thanks in advance!