diff --git a/test/macro_test.py b/test/macro_test.py index a848b272..6e772201 100644 --- a/test/macro_test.py +++ b/test/macro_test.py @@ -1,7 +1,11 @@ -# -*- coding:utf-8 -*- +# -*- coding:utf-8 -*- ''' -Created on 2015/3/14 +Created on 2015/01/24 @author: Jimmy Liu + +Modified on 2020/06/28 +@contributor: Cole ZHANG +@email: longzonejazz@gmail.com ''' import unittest import tushare.stock.macro as fd @@ -29,9 +33,6 @@ def test_get_cpi(self): def test_get_ppi(self): print(fd.get_ppi()) - def test_get_deposit_rate(self): - print(fd.get_deposit_rate()) - def test_get_loan_rate(self): print(fd.get_loan_rate()) @@ -43,8 +44,13 @@ def test_get_money_supply(self): def test_get_money_supply_bal(self): print(fd.get_money_supply_bal()) + + def test_get_total_import_export(self): + print(fd.get_total_import_export()) + + def test_get_industry_fixed_investment(self): + print(fd.get_industry_fixed_investment()) if __name__ == "__main__": - #import sys;sys.argv = ['', 'Test.testName'] - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tushare/__init__.py b/tushare/__init__.py index e14eee42..6896c862 100644 --- a/tushare/__init__.py +++ b/tushare/__init__.py @@ -35,7 +35,9 @@ get_ppi, get_deposit_rate, get_loan_rate, get_rrr, get_money_supply, get_money_supply_bal, - get_gold_and_foreign_reserves) + get_gold_and_foreign_reserves, + get_industry_fixed_investment, + get_total_import_export) """ for classifying data diff --git a/tushare/stock/macro.py b/tushare/stock/macro.py index 4fe043f9..d307c69e 100644 --- a/tushare/stock/macro.py +++ b/tushare/stock/macro.py @@ -6,6 +6,10 @@ @author: Jimmy Liu @group : waditu @contact: jimmysoa@sina.cn + +Modified on 2020/06/28 +@contributor: Cole ZHANG +@email: longzonejazz@gmail.com """ import pandas as pd @@ -248,21 +252,23 @@ def get_deposit_rate(): deposit_type :存款种类 rate:利率(%) """ - rdint = vs.random() - request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'], - rdint, vs.MACRO_TYPE[2], 2, 600, - rdint)) - text = urlopen(request, timeout=10).read() - text = text.decode('gbk') - regSym = re.compile(r'\,count:(.*?)\}') - datastr = regSym.findall(text) - datastr = datastr[0] - datastr = datastr.split('data:')[1] - js = json.loads(datastr) - df = pd.DataFrame(js, columns=vs.DEPOSIT_COLS) - for i in df.columns: - df[i] = df[i].apply(lambda x:np.where(x is None, '--', x)) - return df + return "get_deposit_rate is unavailable" + + # rdint = vs.random() + # request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'], + # rdint, vs.MACRO_TYPE[2], 2, 600, + # rdint)) + # text = urlopen(request, timeout=10).read() + # text = text.decode('gbk') if ct.PY3 else text + # regSym = re.compile(r'\,count:(.*?)\}') + # datastr = regSym.findall(text) + # datastr = datastr[0] + # datastr = datastr.split('data:')[1] + # js = json.loads(datastr) + # df = pd.DataFrame(js, columns=vs.DEPOSIT_COLS) + # for i in df.columns: + # df[i] = df[i].apply(lambda x:np.where(x is None, '--', x)) + # return df def get_loan_rate(): @@ -275,21 +281,22 @@ def get_loan_rate(): loan_type :存款种类 rate:利率(%) """ - rdint = vs.random() - request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'], - rdint, vs.MACRO_TYPE[2], 3, 800, - rdint)) - text = urlopen(request, timeout=10).read() - text = text.decode('gbk') - regSym = re.compile(r'\,count:(.*?)\}') - datastr = regSym.findall(text) - datastr = datastr[0] - datastr = datastr.split('data:')[1] - js = json.loads(datastr) - df = pd.DataFrame(js, columns=vs.LOAN_COLS) - for i in df.columns: - df[i] = df[i].apply(lambda x:np.where(x is None, '--', x)) - return df + return "get_loan_rate is unavailable" + # rdint = vs.random() + # request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'], + # rdint, vs.MACRO_TYPE[2], 3, 800, + # rdint)) + # text = urlopen(request, timeout=10).read() + # text = text.decode('gbk') if ct.PY3 else text + # regSym = re.compile(r'\,count:(.*?)\}') + # datastr = regSym.findall(text) + # datastr = datastr[0] + # datastr = datastr.split('data:')[1] + # js = json.loads(datastr) + # df = pd.DataFrame(js, columns=vs.LOAN_COLS) + # for i in df.columns: + # df[i] = df[i].apply(lambda x:np.where(x is None, '--', x)) + # return df def get_rrr(): @@ -308,7 +315,7 @@ def get_rrr(): rdint, vs.MACRO_TYPE[2], 4, 100, rdint)) text = urlopen(request, timeout=10).read() - text = text.decode('gbk') + text = text.decode('gbk') if ct.PY3 else text regSym = re.compile(r'\,count:(.*?)\}') datastr = regSym.findall(text) datastr = datastr[0] @@ -349,7 +356,7 @@ def get_money_supply(): rdint, vs.MACRO_TYPE[2], 1, 600, rdint)) text = urlopen(request, timeout=10).read() - text = text.decode('gbk') + text = text.decode('gbk') if ct.PY3 else text regSym = re.compile(r'\,count:(.*?)\}') datastr = regSym.findall(text) datastr = datastr[0] @@ -382,7 +389,7 @@ def get_money_supply_bal(): rdint, vs.MACRO_TYPE[2], 0, 200, rdint)) text = urlopen(request,timeout=10).read() - text = text.decode('gbk') + text = text.decode('gbk') if ct.PY3 else text regSym = re.compile(r'\,count:(.*?)\}') datastr = regSym.findall(text) datastr = datastr[0] @@ -409,7 +416,7 @@ def get_gold_and_foreign_reserves(): rdint, vs.MACRO_TYPE[2], 5, 200, rdint)) text = urlopen(request,timeout=10).read() - text = text.decode('gbk') + text = text.decode('gbk') if ct.PY3 else text regSym = re.compile(r'\,count:(.*?)\}') datastr = regSym.findall(text) datastr = datastr[0] @@ -419,3 +426,62 @@ def get_gold_and_foreign_reserves(): for i in df.columns: df[i] = df[i].apply(lambda x: np.where(x is None, '--', x)) return df + + +def get_total_import_export(): + """ + 获取进出口总额 + Returns + ------- + DataFrame + year:统计时间 + total:进出口总额(美元亿元) + export:出口总额(美元亿元) + import:进口总额(美元亿元) + delta:差额(美元亿元) + """ + rdint = vs.random() + request = Request(vs.MACRO_URL % (vs.P_TYPE['http'], vs.DOMAINS['sina'], + rdint, vs.MACRO_TYPE[3], 0, 400, + rdint)) + text = urlopen(request, timeout=10).read() + text = text.decode('gbk') + regSym = re.compile(r'\,count:(.*?)\}') + datastr = regSym.findall(text) + datastr = datastr[0] + datastr = datastr.split(u'\'美元\':')[1] + datastr = datastr.replace("\"", "") + js = json.loads(datastr) + df = pd.DataFrame(js, columns=vs.TOTAL_IMPORT_EXPORT) + for i in df.columns: + df[i] = df[i].apply(lambda x: np.where(x is None, '--', x)) + return df + + +def get_industry_fixed_investment(): + """ + 获取行业固投完成情况 + Returns + ------- + DataFrame + month:统计时间 + industry:行业 + amount:固定资产投资完成额(亿元) + increment:固定资产投资完成额同比增长(%) + """ + rdint = vs.random() + request = Request(vs.MACRO_URL % (vs.P_TYPE['http'], vs.DOMAINS['sina'], + rdint, vs.MACRO_TYPE[4], 10, 10000, + rdint)) + text = urlopen(request, timeout=10).read() + text = text.decode('gbk') + regSym = re.compile(r'\,count:(.*?)\}') + datastr = regSym.findall(text) + datastr = datastr[0] + datastr = datastr.split('data:')[1] + js = json.loads(datastr) + df = pd.DataFrame(js, columns=vs.INDUSTRY_FIXED_INVESTMENT) + for i in df.columns: + df[i] = df[i].apply(lambda x: np.where(x is None, '--', x)) + + return df diff --git a/tushare/stock/macro_vars.py b/tushare/stock/macro_vars.py index 97573895..9a7ce26d 100644 --- a/tushare/stock/macro_vars.py +++ b/tushare/stock/macro_vars.py @@ -2,8 +2,8 @@ P_TYPE = {'http':'http://','ftp':'ftp://'} DOMAINS = {'sina':'sina.com.cn','sinahq':'sinajs.cn','ifeng':'ifeng.com'} -MACRO_TYPE = ['nation','price','fininfo'] -MACRO_URL = '%smoney.finance.%s/mac/api/jsonp.php/SINAREMOTECALLCALLBACK%s/MacPage_Service.get_pagedata?cate=%s&event=%s&from=0&num=%s&condition=&_=%s' +MACRO_TYPE = ['nation','price','fininfo','foreign','fixed'] +MACRO_URL = '%squotes.%s/mac/api/jsonp_v3.php/SINAREMOTECALLCALLBACK%s/MacPage_Service.get_pagedata?cate=%s&event=%s&from=0&num=%s&condition=&_=%s' GDP_YEAR_COLS = ['year','gdp','pc_gdp','gnp','pi','si','industry','cons_industry','ti','trans_industry','lbdy'] GDP_QUARTER_COLS = ['quarter','gdp','gdp_yoy','pi','pi_yoy','si','si_yoy','ti','ti_yoy'] GDP_FOR_COLS = ['year','end_for','for_rate','asset_for','asset_rate','goods_for','goods_rate'] @@ -17,6 +17,8 @@ MONEY_SUPPLY_COLS = ['month','m2','m2_yoy','m1','m1_yoy','m0','m0_yoy','cd','cd_yoy','qm','qm_yoy','ftd','ftd_yoy','sd','sd_yoy','rests','rests_yoy'] MONEY_SUPPLY_BLA_COLS = ['year','m2','m1','m0','cd','qm','ftd','sd','rests'] GOLD_AND_FOREIGN_CURRENCY_RESERVES = ['month','gold','foreign_reserves'] +TOTAL_IMPORT_EXPORT = ['year', 'total', 'export', 'import', 'delta'] +INDUSTRY_FIXED_INVESTMENT = ['month', 'industry', 'amount', 'increment'] def random(n=13): from random import randint