Google の検索窓から出る補完候補を elisp で取得する
Google 検索をするとき、「あ」とか打ったら、「あまぞん.com」とか色々補完候補がでてくるアレを elisp で取得してみよう、と。
ただそれだけです。はい。
下記のような関数をでっちあげてみました。url-http 関連と json.el を使ってます。両者共、Emacs23 ならば標準であります。
特に何かで応用して使おうとか考えてなかったので、そのままカレントバッファに取得した候補を insert してしまいます。
# とりあえず候補を取ってみようという漠然とした目的があって作っただけなので(^_^;
(require 'url-http) (require 'json) (defun my-google-suggest-get-candidates (input) (let* ((url (concat "http://clients1.google.co.jp/complete/search?hl=ja&cp=2&q=" (url-hexify-string (if (null input) "" input)))) (buf (let ((url-request-extra-headers `(("Accept-Encoding" . "identity") ("Content-type" . "application/x-www-form-urlencoded"))) (url-request-method "GET") (url-max-redirections 0)) (url-retrieve-synchronously url))) (json-begp 19) p jsonp json candidates) (setq jsonp (unwind-protect (when (setq p (url-http-symbol-value-in-buffer 'url-http-end-of-headers buf)) (with-current-buffer buf (decode-coding-string (buffer-substring (1+ p) (point-max)) ;; 'utf-8))) 'sjis))) (kill-buffer buf))) (setq json (json-read-from-string (substring jsonp json-begp (1- (length jsonp))))) (setq candidates (aref json 1)) (dotimes (i (length candidates)) ;; とりあえず insert してしまう。 (insert (format "%s\n" (aref (aref candidates i) 0))))))
んでもって、実行例です。
(my-google-suggest-get-candidates "あ") あまぞん.com アマゾン アメブロ 嵐 朝日新聞 アバター アスクル アルク アニメイト amazon (my-google-suggest-get-candidates "とりあえず") とりあえず生中 とりあえずお疲れ とりあえず 意味 とりあえず吾平 とりあえず吾平 メニュー とりあえずマイリスト とりあえず吾平 クーポン とりあえず 敬語 とりあえず 仮 鶏あえず (my-google-suggest-get-candidates "Linux") linux コマンド linux ディストリビューション linux 入門 linux 初心者 linux インストール linux コマンド 一覧 linux find linux 環境変数 linux grep linux cp
激しく誰かが作ってそうな気もしますが、とりあえず作成してみました。
まあ、そんなこんなで。
追記
list を返す版を作成。あんどちょっとコードを修正。
(defun my-google-suggest-get-candidates (&optional input) (let* ((url (concat "http://clients1.google.co.jp/complete/search?hl=ja&cp=2&q=" (url-hexify-string (if (null input) (current-word) input)))) (jsonp (let (buf p) (unwind-protect (progn (setq buf (let ((url-request-extra-headers `(("Accept-Encoding" . "identity") ("Content-type" . "application/x-www-form-urlencoded"))) (url-request-method "GET") (url-max-redirections 0)) (url-retrieve-synchronously url))) (when (setq p (url-http-symbol-value-in-buffer 'url-http-end-of-headers buf)) (with-current-buffer buf (decode-coding-string (buffer-substring (1+ p) (point-max)) ;; 'utf-8)))) 'sjis)))) (when buf (kill-buffer buf))))) (json-begp 19) (json (json-read-from-string (substring jsonp json-begp (1- (length jsonp))))) (candidates-ary (aref json 1)) candidates-lst) (dotimes (i (length candidates-ary)) (setq candidates-lst (cons (aref (aref candidates-ary i) 0) ;; (cons (cons (aref (aref candidates-ary i) 0) (aref (aref candidates-ary i) 1)) candidates-lst))) (nreverse candidates-lst)))
更新時刻
- 2010/01/28/23:00
- 2010/01/28/23:58
- 2010/01/29/15:35