Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 shibor 接口无法获取数据问题 #1172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 28 additions & 20 deletions tushare/stock/shibor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
@group : waditu
@contact: [email protected]
"""
import pandas as pd
"""
Modified on 2019/10/22
@author: Bopo
@contact: [email protected]
"""
from io import BytesIO

import numpy as np
import pandas as pd
import requests
from tushare.stock import cons as ct
from tushare.util import dateu as du
from tushare.util.netbase import Client
from pandas.compat import StringIO


def shibor_data(year=None):
"""
Expand All @@ -35,15 +43,17 @@ def shibor_data(year=None):
year = du.get_year() if year is None else year
lab = ct.SHIBOR_TYPE['Shibor']
lab = lab.encode('utf-8') if ct.PY3 else lab

try:
clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
clt = requests.get(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
ct.PAGES['dw'], 'Shibor',
year, lab,
year))
content = clt.gvalue()
df = pd.read_excel(StringIO(content))
content = clt.content
df = pd.read_excel(BytesIO(content))
df.columns = ct.SHIBOR_COLS
df['date'] = df['date'].map(lambda x: x.date())

if pd.__version__ < '0.21':
df['date'] = df['date'].astype(np.datetime64)
else:
Expand Down Expand Up @@ -85,12 +95,12 @@ def shibor_quote_data(year=None):
lab = ct.SHIBOR_TYPE['Quote']
lab = lab.encode('utf-8') if ct.PY3 else lab
try:
clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
clt = requests.get(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
ct.PAGES['dw'], 'Quote',
year, lab,
year))
content = clt.gvalue()
df = pd.read_excel(StringIO(content), skiprows=[0])
content = clt.content
df = pd.read_excel(BytesIO(content), skiprows=[0])
# df.columns = ct.QUOTE_COLS
df.columns = ct.SHIBOR_Q_COLS
df['date'] = df['date'].map(lambda x: x.date())
Expand Down Expand Up @@ -118,12 +128,12 @@ def shibor_ma_data(year=None):
lab = ct.SHIBOR_TYPE['Tendency']
lab = lab.encode('utf-8') if ct.PY3 else lab
try:
clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
clt = requests.get(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
ct.PAGES['dw'], 'Shibor_Tendency',
year, lab,
year))
content = clt.gvalue()
df = pd.read_excel(StringIO(content), skiprows=[0])
content = clt.content
df = pd.read_excel(BytesIO(content), skiprows=[0])
df.columns = ct.SHIBOR_MA_COLS
df['date'] = df['date'].map(lambda x: x.date())
if pd.__version__ < '0.21':
Expand Down Expand Up @@ -151,12 +161,12 @@ def lpr_data(year=None):
lab = ct.SHIBOR_TYPE['LPR']
lab = lab.encode('utf-8') if ct.PY3 else lab
try:
clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
clt = requests.get(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
ct.PAGES['dw'], 'LPR',
year, lab,
year))
content = clt.gvalue()
df = pd.read_excel(StringIO(content), skiprows=[0])
content = clt.content
df = pd.read_excel(BytesIO(content), skiprows=[0])
df.columns = ct.LPR_COLS
df['date'] = df['date'].map(lambda x: x.date())
if pd.__version__ < '0.21':
Expand Down Expand Up @@ -186,21 +196,19 @@ def lpr_ma_data(year=None):
lab = ct.SHIBOR_TYPE['LPR_Tendency']
lab = lab.encode('utf-8') if ct.PY3 else lab
try:
clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
clt = requests.get(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'],
ct.PAGES['dw'], 'LPR_Tendency',
year, lab,
year))
content = clt.gvalue()
df = pd.read_excel(StringIO(content), skiprows=[0])
content = clt.content
df = pd.read_excel(BytesIO(content), skiprows=[0])
df.columns = ct.LPR_MA_COLS
df['date'] = df['date'].map(lambda x: x.date())

if pd.__version__ < '0.21':
df['date'] = df['date'].astype(np.datetime64)
else:
df['date'] = df['date'].astype('datetime64[D]')
return df
except:
return None