简体   繁体   English

Jsoup股票报价报废Yahoo Finance

[英]Jsoup Stock Quote Scraping Yahoo Finance

Using OracleSQL and Java 使用OracleSQL和Java

I have a "TickerSymbol Database" and a "Stockquote Database". 我有一个“ TickerSymbol数据库”和一个“股票报价数据库”。

Selecting TickerSymbols from "GOOG","APPL","FB", and "AMZN" from the "TickerSymbolDatabase" 从“ TickerSymbolDatabase”中的“ GOOG”,“ APPL”,“ FB”和“ AMZN”中选择TickerSymbols
and circulating the ticker symbols at the end of YahooFinance URL. 并在YahooFinance URL的末尾循环显示股票代码。 http://finance.yahoo.com/q?s= (TICKER) 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 我不确定如何使用Jsoup选择器,或者如何在YahooFinance URL末尾循环股票代码

Here's a simple example. 这是一个简单的例子。 Please check the TOS, and you might prefer Stanley's suggestion of fetching via CSV. 请检查TOS,您可能更喜欢Stanley关于通过CSV进行获取的建议。 I wanted to show how to fetch it in jsoup. 我想展示如何在jsoup中获取它。 Getting it into Oracle is a different question. 将其纳入Oracle是另一个问题。

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. 我喜欢使用Try jsoup来测试和调试URL响应和选择器查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM