Categories: Python

Yahoo!Japan ファイナンス から株価の取得

ふと株価を取得してみたくて試行錯誤していたところ、ちょうど参考になるブログを見つけました。

・Python pandas で日本の株価情報取得とローソク足チャート描画
http://sinhrks.hatenablog.com/entry/2015/02/04/002258

これをそのままやっていくとWarningがでてきたので修正しました。

■環境
Python 3.5.1
Anaconda 4.2.0

pandas.io をインポートすると削除予定と表示される。

C:\Anaconda2\envs\py35\lib\site-packages\pandas\io\data.py:35: FutureWarning: 
The pandas.io.data module is moved to a separate package (pandas-datareader) and will be removed from pandas in a future version.
After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader import data, wb``.
  FutureWarning)

なのでpandas.ioを使わないように修正しました。

# coding: utf-8
from __future__ import unicode_literals
import numpy as np
import pandas as pd
#import pandas.io.data as web
import pandas.tools.plotting as plotting
import datetime as dt


def get_quote_yahoojp(code, start=None, end=None, interval='d'):
    base = 'http://info.finance.yahoo.co.jp/history/?code={0}.T&{1}&{2}&tm={3}&p={4}'
    #start, end = web._sanitize_dates(start, end)
    start = pd.to_datetime(start)
    if end == None:
        end = pd.to_datetime(pd.datetime.now())
    else :
        end = pd.to_datetime(end)
    start = 'sy={0}&sm={1}&sd={2}'.format(start.year, start.month, start.day)
    end = 'ey={0}&em={1}&ed={2}'.format(end.year, end.month, end.day)
    p = 1
    results = []

    if interval not in ['d', 'w', 'm', 'v']:
        raise ValueError("Invalid interval: valid values are 'd', 'w', 'm' and 'v'")

    while True:
        url = base.format(code, start, end, interval, p)
        tables = pd.read_html(url, header=0)
        if len(tables) 

2016/10/2から直近までのマクドナルドの株価を取得する場合

start ='2016-10-02'
mac_tse = get_quote_yahoojp(2702, start=start)
mac_tse.head()

Out[3]: 
            Open  High   Low  Close  Volume  Adj Close
Date                                                  
2016-10-03  2981  3005  2979   2989  177100       2989
2016-10-04  2990  2998  2975   2985  222700       2985
2016-10-05  2980  3010  2980   2984  291900       2984
2016-10-06  2985  2990  2965   2971  259600       2971
2016-10-07  2985  2989  2935   2935  439900       2935

できました。

関連する投稿:

kaz

View Comments

Share
Published by
kaz

Recent Posts

よく使うショートカットキー

土曜日の日経新聞のNIKKEI…

2年 ago

気になるETFのメモ

ETFを色々調べていたので銘柄…

3年 ago

SPDRゴールド・シェアETF(GLD)を買いました

無事に米国株取引口座を開設でき…

3年 ago

インフレになったらどうなるのか

最近、インフレになったらどうな…

3年 ago

複利の話とiDeCoのすすめ

転職した会社には確定拠出年金が…

3年 ago