Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
google financeのAPI使用サンプル
import requests
from datetime import datetime, timedelta
import pandas as pd
url = ""
code = 7203
lsat_date = #データの取得開始日
interval = 86400 #データの間隔(秒)。1日 = 86400秒
market = "TYO" #取引所のコード TYO=東京証券取引所
period = "1Y" #データを取得する期間
params = {
'q': code,
'i': interval,
'x': market,
'p': period,
'ts': lsat_date.timestamp()
r = requests.get(url, params=params)
lines = r.text.splitlines()
columns = lines[4].split("=")[1].split(",")
pridces = lines[8:]
first_cols = pridces[0].split(",")
first_date = datetime.fromtimestamp(int(first_cols[0].lstrip('a')))
result = [[, first_cols[1], first_cols[2], first_cols[3], first_cols[4], first_cols[5]]]
for price in pridces[1:]:
cols = price.split(",")
date = first_date + timedelta(int(cols[0]))
result.append([, cols[1], cols[2], cols[3], cols[4], cols[5]])
df = pd.DataFrame(result, columns = columns)
df.to_csv(str(code) + ".csv", index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment