import json
import urllib.request
import sqlite3
from os import path
stocks = ['AAPL', "MSFT"]
connection = sqlite3.connect("mydb.db")
cursor = connection.cursor()
for stock in stocks:
url = "https://financialmodelingprep.com/api/v3/historical-price-full/" + stock
data = urllib.request.urlopen(url).read().decode()
obj = json.loads(data)
for child in obj['historical']:
print(child)
cursor.execute("Insert into StockPrices values (?, ?, ?, ?, ?, ?, ?)",
(child['date'], child['close'], child['high'], child['low'],
child['open'], child['volume'], stock))
connection.commit()
CREATE TABLE "StockPrices" (
"date" TEXT,
"close" INTEGER,
"high" INTEGER,
"low" INTEGER,
"open" INTEGER,
"volume" INTEGER,
"symbol" TEXT
);
JSON который возвращает API
{
"symbol" : "AAPL",
"historical" : [ {
"date" : "2015-04-22",
"open" : 126.99,
"high" : 128.87,
"low" : 126.32,
"close" : 128.62,
"adjClose" : 119.0,
"volume" : 3.76545E7,
"unadjustedVolume" : 3.76545E7,
"change" : 1.63,
"changePercent" : 1.284,
"vwap" : 127.93667,
"label" : "April 22, 15",
"changeOverTime" : 0.01284
}, {
"date" : "2015-04-23",
"open" : 128.3,
"high" : 130.42,
"low" : 128.14,
"close" : 129.67,
"adjClose" : 119.97,
"volume" : 4.57709E7,
"unadjustedVolume" : 4.57709E7,
"change" : 1.37,
"changePercent" : 1.068,
"vwap" : 129.41,
"label" : "April 23, 15",
"changeOverTime" : 0.01068
}
}