query_cashflow_data.py
sh:上海,sz: 深圳 ,?
# -*- coding: utf-8 -*-
import os, sys
import baostock as bs
import pandas as pd
if len(sys.argv) ==3:
sz = sys.argv[1]
code = sys.argv[2]
else:
print('usage: python query_cashflow_data.py sh stockcode ')
print('usage: python query_cashflow_data.py sz stockcode ')
sys.exit(1)
if len(code) !=6:
print('stock code length: 6')
sys.exit(2)
# 登入系统
lg = bs.login()
#lg = bs.login(user_id="anonymous", password="123456")
# 显示登陆返回信息
print('login respond error_code:'+lg.error_code)
#print('login respond error_msg:'+lg.error_msg)
stockcode = sz+'.'+code
print(stockcode)
# 中文字段名
cash_fields =['证券代码','发布财报的日期','财报统计的季度日期','流动资产除以总资产','非流动资产除以总资产','有形资产除以总资产','已获利息倍数','经营产生的现金流量净额除以营业收入','经营性现金净流量除以净利润','经营性现金净流量除以营业总收入']
# 季频现金流量
cash_flow_list = []
for y in range(2010,2022):
rs_cash_flow = bs.query_cash_flow_data(code=stockcode, year=y, quarter=4)
while (rs_cash_flow.error_code == '0') & rs_cash_flow.next():
cash_flow_list.append(rs_cash_flow.get_row_data())
# 今年一季度
rs_cash_flow = bs.query_cash_flow_data(code=stockcode, year=2022, quarter=1)
while (rs_cash_flow.error_code == '0') & rs_cash_flow.next():
cash_flow_list.append(rs_cash_flow.get_row_data())
# 结果集转化为 pd.DataFrame
result_cash_flow = pd.DataFrame(cash_flow_list, columns=cash_fields)
# 打印输出原表头
#print(rs_cash_flow.fields)
# 结果集输出到csv文件
csvfile = "D:/Investor/cash_"+code+".csv"
result_cash_flow.to_csv(csvfile, encoding="gbk", index=False)
# 登出系统
bs.logout()
运行 python?query_cashflow_data.py sz 000661
参考:?企业季频现金流量
|