anonymous No title
No License Python
2021年07月24日
Copy Clone
import sys
import numpy
from yahoo_finance_api2 import share
from yahoo_finance_api2.exceptions import YahooFinanceError
import pandas as pd
import time

print('銘柄コードの上位1ケタを入力してください')
user_input_num = input()

up1_num = int(user_input_num)

f = open(f'./sql/basedata_{up1_num}xxx.sql', 'w')
for num in range(up1_num*1000, (up1_num+1)*1000):
    print(f'銘柄コード:{num}')
    my_share = share.Share(f'{num}.T')
    symbol_data = None

    try:
        symbol_data = my_share.get_historical(
            share.PERIOD_TYPE_YEAR, 50,
            share.FREQUENCY_TYPE_DAY, 1)
        print('データ取得成功')
        df = pd.DataFrame(symbol_data)
        if df.empty:
            continue
        df["datetime"] = pd.to_datetime(df.timestamp, unit="ms")
        for record in df.values:
            print(f'銘柄コード:{num} 日付:{record[6]} 始値:{record[1]} 高値:{record[2]} 安値:{record[3]} 終値:{record[4]} 出来高:{record[5]}')
            if numpy.isnan(record[1]) or numpy.isnan(record[2]) or numpy.isnan(record[3]) or numpy.isnan(record[4]):
                continue
            f.write(f'INSERT INTO `my_stock_database` VALUES (null, \'{num}\', \'{record[6]}\', {record[1]}, {record[2]}, {record[3]}, {record[4]}, {record[5]});\n')

    except YahooFinanceError as e:
        print('データ取得失敗')
        print(e.message)
    time.sleep(1)
f.close()

import sys
import numpy
from yahoo_finance_api2 import share
from yahoo_finance_api2.exceptions import YahooFinanceError
import pandas as pd
import time

print('銘柄コードの上位1ケタを入力してください')
user_input_num = input()

up1_num = int(user_input_num)

f = open(f'./sql/basedata_{up1_num}xxx.sql', 'w')
for num in range(up1_num*1000, (up1_num+1)*1000):
    print(f'銘柄コード:{num}')
    my_share = share.Share(f'{num}.T')
    symbol_data = None

    try:
        symbol_data = my_share.get_historical(
            share.PERIOD_TYPE_YEAR, 50,
            share.FREQUENCY_TYPE_DAY, 1)
        print('データ取得成功')
        df = pd.DataFrame(symbol_data)
        if df.empty:
            continue
        df["datetime"] = pd.to_datetime(df.timestamp, unit="ms")
        for record in df.values:
            print(f'銘柄コード:{num} 日付:{record[6]} 始値:{record[1]} 高値:{record[2]} 安値:{record[3]} 終値:{record[4]} 出来高:{record[5]}')
            if numpy.isnan(record[1]) or numpy.isnan(record[2]) or numpy.isnan(record[3]) or numpy.isnan(record[4]):
                continue
            f.write(f'INSERT INTO `my_stock_database` VALUES (null, \'{num}\', \'{record[6]}\', {record[1]}, {record[2]}, {record[3]}, {record[4]}, {record[5]});\n')

    except YahooFinanceError as e:
        print('データ取得失敗')
        print(e.message)
    time.sleep(1)
f.close()

No one still commented. Please first comment.
Output