【问题标题】:Getting stock prices from Yahoo with Elisp?使用 Elisp 从 Yahoo 获取股票价格?
【发布时间】:2010-01-10 13:41:46
【问题描述】:

我想使用 Yahoo 从 Emacs Lisp 程序中获取股票价格。我有两个问题。

  1. 如何进行 http GET?
  2. 在 Elisp 中存储数据以便比较数据的最佳方法是什么?换句话说,我应该使用一个哈希表、几个哈希表还是列表来表示从雅虎返回的数据?

这是我想做的事情的基本大纲。

;;致电雅虎获取股票价格 ;; ;;雅虎输入: ;; http://download.finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG&f=sb2b3jkm6 ;;雅虎输出: ;; “苹果”,211.98,211.82,78.20,215.59,+17.90% ;; “谷歌”,602.94,601.69,282.75,629.51,+18.27% ;; ;;交易品种、要价、出价、52 周低点、52 周高点、200 天平均值的百分比变化 ;; ;;此处描述的雅虎格式:http://www.gummy-stuff.org/Yahoo-data.htm (defun get-price-url (代码) " s = 符号 b2 = 实时询问 b3 = 实时出价 j = 52 周低点 k = 52 周高点 " (concat "http://download.finance.yahoo.com/d/quotes.csv?s=" (mapconcat '身份代码 "+") "&f=sb2b3jk")) (setq lst '("AAPL" "GOOG" "MSFT" "ORCL")) (setq url (get-price-url lst)) ;;使用 Url 调用 Yahoo,处理结果并放入数据结构中 ;; ;;返回结果按200天mavg最大变化排序,降序排列 ;;

【问题讨论】:

  • 感谢您向我介绍 `mapconcat',这很方便!

标签: elisp


【解决方案1】:

这里有一些代码可以帮助您入门;我展示了如何将 url 抓取到缓冲区,解析每一行,然后显示每个项目的代码和价格。您可以从那里修改它以执行您需要的操作。

这会将每行股票数据解析为一个列表,并且可以直接使用第一个、第二个、第三个函数或使用 nth 来获取值。您可以编写函数来获取您想要的每个元素,例如 get-ticker(quote),它只会返回 (first ticker)

我不会过多考虑使用哪种数据结构;任何最简单的都可以。如果你需要高性能,那么无论如何你都不应该使用 emacs lisp。

(defun test()
  (interactive)
  (let ((quotes (get-quotes '("AAPL" "GOOG" "MSFT" "ORCL" "ERTS" "THQI") "sb")))
    (show-quotes quotes)))

(defun show-quotes(quotes)
  (dolist (quote quotes)
    (message (format "%s $%.2f" (first quote) (string-to-number (second quote))))))

(defun get-quotes(tickers field-string)
  "Given a list of ticker names and a string of fields to return as above, this grabs them
from Yahoo, and parses them"
  (let ((results-buffer (get-yahoo-quotes-to-buffer (get-price-url tickers field-string))))
    (switch-to-buffer results-buffer)
    (parse-quote-buffer results-buffer)))

(defun get-price-url (tickers field-string)
  "Set up the get url"
  (concat "http://download.finance.yahoo.com/d/quotes.csv?s=" 
      (mapconcat 'identity tickers "+") 
      "&f=" field-string))

(defun get-yahoo-quotes-to-buffer(url)
  "Retrieve the quotes to a buffer and return it"
  (url-retrieve-synchronously url))

(defun parse-quote-buffer(b)
  "Parse the buffer for quotes"
  (goto-line 1)
  (re-search-forward "^\n")
  (beginning-of-line)
  (let ((res nil))
    (while (> (point-max) (point))
      (setf res (cons  (split-string (thing-at-point 'line) ",") res))
      (forward-line 1))
    (reverse res)))

【讨论】:

    【解决方案2】:

    查看http://edward.oconnor.cx/elisp/。 Edward 有一些使用 HTTP 与各种服务交互的示例,如果您找不到 Yahoo 客户端库,您可以使用这些技术编写一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 2020-07-08
      • 2019-06-27
      • 2020-11-28
      • 2017-01-08
      • 2020-04-21
      • 2011-03-23
      • 1970-01-01
      相关资源
      最近更新 更多