【Python Program】資本金、決算期、決算発表日をチェック

資本金、決算期、決算発表日をチェックするPythonプログラム

銘柄選び(その2)「ストップ高銘柄の候補」で資本金、決算期、決算発表日をチェックするプログラムです。

#資本金チェック
def fin_chk(s_code):
    from urllib import request
    from bs4 import BeautifulSoup
    import warnings
    warnings.simplefilter('ignore')
    url = "https://xn--vckya7nx51ik9ay55a3l3a.com/searches/result?utf8=%E2%9C%93&search_query=" + s_code
    response = request.urlopen(url)
    soup = BeautifulSoup(response)
    response.close()
    sihon = soup.find_all('p', class_='col-8 right')
    if len(sihon) == 0:
        return 0
    txt_s = sihon[1].text
    l_fin = txt_s.find("百万円")
    k_txt = txt_s[:l_fin]
    if k_txt == '\n\t\t\t\t':
        return 0
    while k_txt.find(",") != -1:
        n_fin = k_txt.find(",")
        k_txt = k_txt[:n_fin] + k_txt[n_fin+1:]
    return(int(k_txt))

引数「s_code」に証券コードを渡すと、資本金が返されます。

#決算期チェック
def kessanki(s_code):
    import pandas as pd
    url = "https://stocks.finance.yahoo.co.jp/stocks/profile/?code=" + s_code + ".T"
    dfs = pd.read_html(url, match = '決算')
    return(dfs[0].iloc[10,1])

引数「s_code」に証券コードを渡すと、決算期が返されます。

#決算発表日
def kessan_happyo(s_code):
    import datetime
    import pandas as pd
    url = "https://www.nikkei.com/markets/kigyo/money-schedule/kessan/?ResultFlag=3&kwd=" + s_code
    try:
        dfs = pd.read_html(url, match = '決算発表日')
    except:
        return datetime.date(1900, 1, 1)
    txt = dfs[0].iloc[0, 0]
    if txt == "--":
        return datetime.date(1900, 1, 1)
    l1 = txt.find("/")
    l2 = txt.find("/", l1 + 1)
    syer = int(txt[:4])
    smon = int(txt[l1 + 1:l2])
    if txt[l2 + 1:] == "上旬":
        sday = 5
    else:
        if txt[l2 + 1:] == "中旬":
            sday = 15
        else:
            if txt[l2 + 1:] == "下旬":
                sday = 25
            else:
                if txt[l2 + 1:] == "末日":
                    sday = 30
                else:
                    sday = int(txt[l2 + 1:])

    kes_d = datetime.date(syer, smon, sday)
    return(kes_d)

引数「s_code」に証券コードを渡すと、決算発表日が返されます。

上記3つのコードを「finance_chk.py」というファイル名で保存しておきます。

Posted by Ish