<返回更多

Python 获取上市公司利润表数据

2022-11-23  今日头条  Pgabc
加入收藏
"""
Python/ target=_blank class=infotextkey>Python 获取上市公司财务利润表数据
"""
import time
import datetime
import tushare as ts
#注册获取token
ts.set_token('your token')
pro = ts.pro_api()

def get_data_income(ts_code='', start_date='', end_date='', retry_count=3, pause=2):
    #获取上市公司财务利润表数据
    for g in range(retry_count):
        try:
            fiel1 = 'ts_code,ann_date,f_ann_date,end_date,report_type,comp_type,end_type,basic_eps,'
            fiel2 = 'diluted_eps,total_revenue,revenue,int_income,prem_earned,comm_income,'
            fiel3 = 'n_commis_income,n_oth_income,n_oth_b_income,prem_income,out_prem,une_prem_reser,'
            fiel4 = 'reins_income,n_sec_tb_income,n_sec_uw_income,n_asset_mg_income,oth_b_income,'
            fiel5 = 'fv_value_chg_gain,invest_income,ass_invest_income,forex_gain,total_cogs,'
            fiel6 = 'oper_cost,int_exp,comm_exp,biz_tax_surchg,sell_exp,admin_exp,fin_exp,'
            fiel7 = 'assets_impair_loss,prem_refund,compens_payout,reser_insur_liab,div_payt,'
            fiel8 = 'reins_exp,oper_exp,compens_payout_refu,insur_reser_refu,reins_cost_refund,'
            fiel9 = 'other_bus_cost,operate_profit,non_oper_income,non_oper_exp,nca_disploss,'
            fiel10 = 'total_profit,income_tax,n_income,n_income_attr_p,minority_gain,oth_compr_income,'
            fiel11 = 't_compr_income,compr_inc_attr_p,compr_inc_attr_m_s,ebit,ebitda,insurance_exp,'
            fiel12 = 'undist_profit,distable_profit,rd_exp,fin_exp_int_exp,fin_exp_int_inc,'
            fiel13 = 'transfer_surplus_rese,transfer_housing_imprest,transfer_oth,adj_lossgain,'
            fiel14 = 'withdra_legal_surplus,withdra_legal_pubfund,withdra_biz_devfund,'
            fiel15 = 'withdra_rese_fund,withdra_oth_ersu,workers_welfare,distr_profit_shrhder,'
            fiel16 = 'prfshare_payable_dvd,comshare_payable_dvd,capit_comstock_div,'
            fiel17 ='.NET_after_nr_lp_correct,credit_impa_loss,net_expo_hedging_benefits,'
            fiel18 = 'oth_impair_loss_assets,total_opcost,amodcost_fin_assets,oth_income,'
            fiel19 = 'asset_disp_income,continued_net_profit,end_net_profit,update_flag'
            fiel0 = fiel1+fiel2+fiel3+fiel4+fiel5+fiel6+fiel7+fiel8+fiel9+fiel10+fiel11+fiel12+fiel13+fiel14+fiel15+
                    fiel16+fiel17+fiel18+fiel19
            df = pro.income(ts_code=ts_code, start_date=start_date, end_date=end_date, fields=fiel0)
        except Exception as err:
            time.sleep(pause)
        else:
            return df

def date_func(days=0):
    try:
        time_temp = datetime.datetime.now() - datetime.timedelta(days=days)
        dt = time_temp.strftime('%Y%m%d')
        return dt
    except Exception as err:
        print('end_dt is err', err)


if __name__ == '__main__':
    # 日期格式为 YYYYMMDD 如 20221122
    # 行情终止日期
    end_dt = date_func(days=0)
    start_dt = date_func(days=365)
    print('start_dt,end_dt:', start_dt, end_dt)
    # 以佛慈制药为例
    code_wm = '002644.SZ'

    # 获取南岭民爆一年的利润表数据数据
    df_wm = get_data_income(ts_code=code_wm, start_date=start_dt, end_date=end_dt, retry_count=3, pause=2)
    print(df_wm.head(8))

    """运行结果
    start_dt,end_dt: 20211122 20221122
         ts_code  ann_date f_ann_date  ...  total_opcost amodcost_fin_assets update_flag
    0  002644.SZ  20221027   20221027  ...  6.479623e+08                None           1
    1  002644.SZ  20220825   20220825  ...  4.089990e+08                None           1
    2  002644.SZ  20220427   20220427  ...  2.114988e+08                None           1
    3  002644.SZ  20220427   20220427  ...  2.114988e+08                None           0
    4  002644.SZ  20220316   20220316  ...  7.528933e+08                None           1
    
    [5 rows x 94 columns]
    
    Process finished with exit code 0
    """""" 
    Pgabc 2022000036 
    更多函数,安装工具包
    pip install pgabc
    pgabc@sohu.com
    www.wmdbsoft.com
    """
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>