0

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!

  • 1
    Take a look at [jsoup](https://jsoup.org/) – lance-java Nov 09 '17 at 15:59
  • 1
    Use an html parser or better, the api provided by yahoo. You should also read [ask] and [mcve], dumping code in and expecting someone to debug it for you doesn't really make for a good [so] question. – pvg Nov 09 '17 at 16:01
  • Will take a look, thank you. And thanks for the reply, still getting used to posing questions on this. Apologies. – Brandon Dooley Nov 09 '17 at 17:10
  • You might want to look at this before you go any further, it's being discussed on meta https://meta.stackoverflow.com/questions/358883/yahoo-recently-pulled-the-plug-on-their-stocks-api-what-are-yahoo-finance-rel – Funk Forty Niner Nov 10 '17 at 00:00

0 Answers0