我正在设计一个纯粹出于个人兴趣和个人用途的程序。我试图提取股票的股票价格,即" GOOG"来自https://finance.yahoo.com/。
我偶然发现普林斯顿提供的Java class似乎正在完成我正在寻找的工作。但是,我知道网站会定期更改,包括HTML源代码。
自编写程序以来,HTML源代码可能已经发生了变化,我正在寻找一些帮助,找到从HTML代码中提取股价的确切位置。普林斯顿的实施使用以下方法:
// 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(",", ""));
}
此刻,将其抛弃的部分很可能是划界线:
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);
我不知疲倦地搜索HTML源代码,似乎无法找到准确定位的位置 - &gt;非常感谢所提供的任何帮助。
当前Java错误:
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)
提前致谢!