1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonでYahoo Financeしてみた

Last updated at Posted at 2023-05-29

以下のような株アプリを作ってる

yfinanceのインストール

yfinanceはpythonのでYahoo Financeにアクセスするためのライブラリです。個人利用しか出来ないです。

pipをインストールして、yahoo finance ライブラリをインストールします。

   # apt install pip
   # pip install yfinance

テーブル作成

json型を使います。

    create database finance;
    CREATE USER 'finance'@'localhost' IDENTIFIED BY 'xxxxx';
    GRANT ALL PRIVILEGES ON * . * TO 'finance'@'localhost';
    use finance;
    CREATE TABLE YahooFINANCE (
      t_date date NOT NULL,
      ticker VARCHAR(255) NOT NULL,
      attr JSON,
      CHECK (JSON_VALID(attr)),
      PRIMARY KEY (t_date,ticker)
   );

GOOGLEの情報をデータベースにinsertし、selectするpy

import mysql.connector
import yfinance as yf
import json

ticker = yf.Ticker("GOOG")

conn = mysql.connector.connect(
    user="finance",
    password="xxxxx",
    host="localhost",
    database="finance")
cur = conn.cursor() 

sql = ('INSERT INTO YahooFINANCE (t_date, ticker, attr) VALUES (now(6), %s, %s) ON DUPLICATE KEY UPDATE attr = VALUES (attr);')

data = [
     ('GOOG', json.dumps(ticker.info)) 
]

cur.executemany(sql, data)

conn.commit() 

cur.execute("SELECT t_date,ticker FROM YahooFINANCE", ()) 

for id,ticker in cur: 
    print(f"TICKER: {ticker}, ID: {id}")

conn.commit() 
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?