(O+P)ut

OutPut Log by SE in SIer



(O+P)ut

Output Log by SE

【Linux】為替情報をwgetコマンドで取得する

スポンサーリンク

はじめに

ドル円レートをスクリプトベースで取得したい、と思った際にネットを見ていると
ISホールディングの外為オンラインが提供する下記のリンク先がありました。

こちらをブラウザで見ると以下のように表示されます。

{"quotes":[{"high":"1.9448","open":"1.9380","bid":"1.9416","currencyPairCode":"GBPNZD","ask":"1.9433","low":"1.9378"},{"high":"80.61","open":"80.58","bid":"80.34","currencyPairCode":"CADJPY","ask":"80.39","low":"80.18"},
...

本記事ではこれらをLinux機からコマンドベースで取得するワンライナーを紹介します。

コマンド実行環境
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"

為替情報の取得ワンライナー

(下記コマンドの実行頻度等、不正アクセスと疑われないようご注意ください)

$ wget -O - -U "" http://www.gaitameonline.com/rateaj/getrate 2> /dev/null

上記で取得でき、実際の実行結果は以下です。

{"quotes":[{"high":"1.9448","open":"1.9380","bid":"1.9421","currencyPairCode":"GBPNZD","ask":"1.9438","low":"1.9378"},{"high":"80.61","open":"80.58","bid":"80.48","currencyPairCode":"CADJPY","ask":"80.53","low":"80.18"},{"high":"1.8193","open":"1.8137","bid":"1.8163","currencyPairCode":"GBPAUD","ask":"1.8172","low":"1.8136"},{"high":"72.28","open":"72.24","bid":"72.14","currencyPairCode":"AUDJPY","ask":"72.17","low":"71.82"},{"high":"1.0693","open":"1.0681","bid":"1.0688","currencyPairCode":"AUDNZD","ask":"1.0698","low":"1.0673"},{"high":"1.4658","open":"1.4623","bid":"1.4639","currencyPairCode":"EURCAD","ask":"1.4646","low":"1.4615"},{"high":"1.0989","open":"1.0969","bid":"1.0983","currencyPairCode":"EURUSD","ask":"1.0984","low":"1.0968"},{"high":"67.61","open":"67.58","bid":"67.44","currencyPairCode":"NZDJPY","ask":"67.50","low":"67.18"},{"high":"1.3343","open":"1.3328","bid":"1.3329","currencyPairCode":"USDCAD","ask":"1.3335","low":"1.3324"},{"high":"0.8995","open":"0.8984","bid":"0.8991","currencyPairCode":"EURGBP","ask":"0.8995","low":"0.8973"},{"high":"1.2215","open":"1.2201","bid":"1.2212","currencyPairCode":"GBPUSD","ask":"1.2215","low":"1.2199"},{"high":"7.009","open":"7.008","bid":"6.986","currencyPairCode":"ZARJPY","ask":"7.136","low":"6.950"},{"high":"1.0925","open":"1.0924","bid":"1.0910","currencyPairCode":"EURCHF","ask":"1.0915","low":"1.0897"},{"high":"108.02","open":"107.89","bid":"108.00","currencyPairCode":"CHFJPY","ask":"108.05","low":"107.71"},{"high":"0.6725","open":"0.6721","bid":"0.6721","currencyPairCode":"AUDUSD","ask":"0.6723","low":"0.6709"},{"high":"0.9957","open":"0.9957","bid":"0.9933","currencyPairCode":"USDCHF","ask":"0.9937","low":"0.9920"},{"high":"117.96","open":"117.89","bid":"117.88","currencyPairCode":"EURJPY","ask":"117.90","low":"117.56"},{"high":"1.2155","open":"1.2149","bid":"1.2130","currencyPairCode":"GBPCHF","ask":"1.2139","low":"1.2114"},{"high":"1.7491","open":"1.7427","bid":"1.7465","currencyPairCode":"EURNZD","ask":"1.7477","low":"1.7422"},{"high":"0.6291","open":"0.6289","bid":"0.6283","currencyPairCode":"NZDUSD","ask":"0.6289","low":"0.6274"},{"high":"107.49","open":"107.47","bid":"107.33","currencyPairCode":"USDJPY","ask":"107.34","low":"107.03"},{"high":"1.6366","open":"1.6307","bid":"1.6334","currencyPairCode":"EURAUD","ask":"1.6342","low":"1.6304"},{"high":"0.6694","open":"0.6694","bid":"0.6676","currencyPairCode":"AUDCHF","ask":"0.6682","low":"0.6657"},{"high":"131.27","open":"131.15","bid":"131.07","currencyPairCode":"GBPJPY","ask":"131.10","low":"130.71"}]}

以下、補足です。

結果の読み方

それぞれのラベルと意味の対応は以下です。

high 高値
open 始値
bid 売り気配値
currencyPairCode 為替コード
ask 買い気配値
low 安値

関係式としては一般的に
high > bid > ask > low が成り立ちます。

ちなみに米ドル⇔日本円のcurrencyPairCodeは「USDJPY」です。

スクリプトの解説

単純に以下で実行すると

$ wget -O - http://www.gaitameonline.com/rateaj/getrate 2> /dev/null

以下のエラーとなります。

<html>
<head><title>Request Rejected</title></head>
<body>セキュリティの制限によりご利用できません。<br>
以下の内容をお試しの上、ご利用できない場合は、
...
・プロキシサーバを利用している場合は設定解除<br>
・PCを再起動<br>
・ルータを再起動<br><br>
Restriction of security can't use it.<br>
When you're trying the following contents and I can't use it.<br>
You cut down support ID and please make a contact to our man to contact.<br><br>
...
・ When using a proxy server, setting is released.<br>
・ A PC is restarted.<br>
・ A router is restarted.<br>

ただこれは過去に心当たりがあります。

要はボットのような利用を禁止してか、ユーザエージェントを入れないとエラーになる模様です。
よって上記コマンドでアクセスを行う場合も節度ある利用が求められます。

終わりに

米国株を購入する機会があり、為替情報に興味が出たので自動化の一歩としてタイトルの件を実装しました。
特定の箇所だけ利用したい場所はgrepなりawkなりを駆使して切り出してみてください。

以上、ご参考になれば幸いです。


他の記事を読む