Skip to content

Instantly share code, notes, and snippets.

@YoshihitoAso
Created October 20, 2015 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshihitoAso/c4dd7c3421ab55730f32 to your computer and use it in GitHub Desktop.
Save YoshihitoAso/c4dd7c3421ab55730f32 to your computer and use it in GitHub Desktop.
[Python]全ETF銘柄の株価データをYahooファイナンスからスクレイピング→CSVファイルに変換
#!/usr/local/bin/python
# -*- coding:utf-8 -*-
import jsm
import datetime
import time
import csv
import pandas as pd
#--------------------------------------------------
# 全ETF銘柄の株価データをYahooファイナンスから取得
# → CSVファイルに変換
#
# Copyright 2015 Yoshihito Aso
#--------------------------------------------------
def price_to_csvl(ccode,price):
# PriceデータをCSV出力用フォーマットに変換
return [ccode, price.date.strftime('%Y-%m-%d'),
price.open, price.high, price.low,
price.close, price.volume, price._adj_close]
if __name__ == "__main__":
out_file = "ETF_Stock_Prices_Daily_20050901-20151001.csv"
c = csv.writer(open(out_file,'a'))
c.writerow(["stock_code","date","open","high","low","close","volume","adj_close"])
#データ取得期間(最大) : 上場以降のデータしか存在しないので注意
start_date = datetime.date(2005,9,1)
end_date = datetime.date(2015,10,1)
df = pd.read_csv('ETF_list.csv')
stock_list = df['stock_code']
for stock_code in stock_list:
time.sleep(10.0) #サイトに負荷をかけ過ぎないように 10sec SLEEP
print stock_code
try:
q = jsm.Quotes()
historical_prices = q.get_historical_prices(stock_code, jsm.DAILY,
start_date = start_date, end_date = end_date)
for price in historical_prices:
c.writerow(price_to_csvl(stock_code, price))
except(jsm.exceptions.CCODENotFoundException): #銘柄が存在しない場合は何もせずに処理継続
pass
@YoshihitoAso
Copy link
Author

ETF_list.csv は自分で準備

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment