-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDay8_saveData.py
38 lines (32 loc) · 1 KB
/
Day8_saveData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
import json
import numpy
import pandas
# API位置
start_time = 946656000 # 2000/1/1
end_time = 1600272000 # 2020/9/17
stock_code = 2317
stock_market = "TW"
address = f"https://query1.finance.yahoo.com/v8/finance/chart/{stock_code}.{stock_market}?period1={start_time}&period2={end_time}&interval=1d&events=history&=hP2rOschxO0"
# 使用requests 來跟遠端 API server 索取資料
response = requests.get(address)
# 序列化資料回報
data = json.loads(response.text)
# 把json格式資料放入pandas中
df = pandas.DataFrame(
data["chart"]["result"][0]["indicators"]["quote"][0],
index=pandas.to_datetime(
numpy.array(data["chart"]["result"][0]["timestamp"]) * 1000 * 1000 * 1000
),
columns=["open", "high", "low", "close", "volume"],
)
# 印出前3行
print(df[:3])
# 印出前5行
print(df.head())
# 印出後5行
print(df.tail())
# 寫成csv
df.tail().to_csv(f"{stock_code}_最近五天.csv")
df.to_csv(f"{stock_code}_{start_time}_{end_time}.csv")
print("===finished===")