user3003451
user3003451

Reputation: 429

Jsoup Stock Quote Scraping Yahoo Finance

Using OracleSQL and Java

I have a "TickerSymbol Database" and a "Stockquote Database".

Selecting TickerSymbols from "GOOG","APPL","FB", and "AMZN" from the "TickerSymbolDatabase"
and circulating the ticker symbols at the end of YahooFinance URL. http://finance.yahoo.com/q?s= (TICKER)

Then finding the stock quote, and inserting the quote data into the "Stockquote Database".

I'm not exactly sure how to use the Jsoup selector, or how to circulate the ticker symbols at the end of the YahooFinance URL

Upvotes: 1

Views: 3147

Answers (2)

Jonathan Hedley
Jonathan Hedley

Reputation: 10522

Here's a simple example. Please check the TOS, and you might prefer Stanley's suggestion of fetching via CSV. I wanted to show how to fetch it in jsoup. Getting it into Oracle is a different question.

String[] codes = {"TSLA", "F", "TM"};
String baseUrl = "http://finance.yahoo.com/q?s=";
String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1438.7 Safari/537.33";

for (String code : codes) {
    String url = baseUrl + code;
    Document doc  = Jsoup.connect(url).userAgent(ua).timeout(10*1000).get();
    String price = doc.select(".time_rtq_ticker").first().text();
    String name = doc.select(".title h2").first().text();

    System.out.println(String.format("%s [%s] is trading at %s", name, code, price));
}

This outputs:

Tesla Motors, Inc. (TSLA) [TSLA] is trading at 135.45
Ford Motor Co. (F) [F] is trading at 17.07
Toyota Motor Corporation (TM) [TM] is trading at 127.98

I like to use Try jsoup to test and debug URL responses and selectors queries.

Upvotes: 1

Related Questions