コマンドラインからgoogle検索
google検索をコマンドラインから実行する方法というとgoogleが公式で出しているSearch APIを使うのが正攻法だと思うけど、API Keyとか用意しないといけなくてクイックにやりたい時には面倒。
で、そんな時にgooglerを使うとサクッとできるので、その方法を書いていく。
インストール
MacOSならbrewで簡単にインストールできる。googlerの結果をJSONで出力するので、JSON操作用にjqも一緒に入れておく。
% brew install googler
% brew install jq
使い方
「アルプス」という単語で検索の一番初めの検索結果(URL)をJSONで出力するには以下のコマンド。
% googler --json -n1 'アルプス'
[
{
"abstract": "スーパーアルプスは、八王子市、日野市、あきる野市、羽村市、町田市、神奈川県、埼玉県で展開するスーパーマーケットです。",
"title": "スーパーアルプス",
"url": "http://superalps.info/"
}
]
URL部分だけ抜き出したいときはjqを使うと良い。
% googler --json -n1 'アルプス' | jq -r '.[].url'
http://superalps.info/
その他はヘルプを見る。
% googler -h
usage: googler [-h] [-s N] [-n N] [-N] [-V] [-c TLD] [-l LANG] [-x]
[--colorize [{auto,always,never}]] [-C] [--colors COLORS] [-j]
[-t dN] [--from FROM] [--to TO] [-w SITE] [--unfilter]
[-p PROXY] [--noua] [--notweak] [--json] [--url-handler UTIL]
[--show-browser-logs] [--np] [-v] [-d]
[KEYWORD [KEYWORD ...]]
Google from the command-line.
positional arguments:
KEYWORD search keywords
optional arguments:
-h, --help show this help message and exit
-s N, --start N start at the Nth result
-n N, --count N show N results (default 10)
-N, --news show results from news section
-V, --videos show results from videos section
-c TLD, --tld TLD country-specific search with top-level domain .TLD,
e.g., 'in' for India
-l LANG, --lang LANG display in language LANG
-x, --exact disable automatic spelling correction
--colorize [{auto,always,never}]
whether to colorize output; defaults to 'auto', which
enables color when stdout is a tty device; using
--colorize without an argument is equivalent to
--colorize=always
-C, --nocolor equivalent to --colorize=never
--colors COLORS set output colors (see man page for details)
-j, --first, --lucky open the first result in web browser and exit
-t dN, --time dN time limit search [h5 (5 hrs), d5 (5 days), w5 (5
weeks), m5 (5 months), y5 (5 years)]
--from FROM starting date/month/year of date range; must use
American date format with slashes, e.g., 2/24/2020,
2/2020, 2020; can be used in conjuction with --to,
and overrides -t, --time
--to TO ending date/month/year of date range; see --from
-w SITE, --site SITE search a site using Google
--unfilter do not omit similar results
-p PROXY, --proxy PROXY
tunnel traffic through an HTTP proxy; PROXY is of the
form [http://][user:password@]proxyhost[:port]
--noua legacy option (no effect)
--notweak disable TCP optimizations and forced TLS 1.2
--json output in JSON format; implies --noprompt
--url-handler UTIL custom script or cli utility to open results
--show-browser-logs do not suppress browser output (stdout and stderr)
--np, --noprompt search and exit, do not prompt
-v, --version show program's version number and exit
-d, --debug enable debugging
omniprompt keys:
n, p fetch the next or previous set of search results
index open the result corresponding to index in browser
f jump to the first page
o [index|range|a ...] open space-separated result indices, numeric ranges
(sitelinks unsupported in ranges), or all, in browser
open the current search in browser, if no arguments
O [index|range|a ...] like key 'o', but try to open in a GUI browser
g keywords new Google search for 'keywords' with original options
should be used to search omniprompt keys and indices
c index copy url to clipboard
u toggle url expansion
q, ^D, double Enter exit googler
? show omniprompt help
* other inputs issue a new search with original options
Version 4.1
Copyright © 2008 Henri Hakkinen
Copyright © 2015-2020 Arun Prakash Jana <engineerarun@gmail.com>
Zhiming Wang <zmwangx@gmail.com>
License: GPLv3
Webpage: https://github.com/jarun/googler
結構細かいことまでカバーできそうだ。