In [1]:
'''
Stocks:
COMMON_SSE_ZQPZ_GPLB_MCJS_SSAG_L A股
COMMON_SSE_ZQPZ_GPLB_MCJS_SSBG_L B股
COMMON_SSE_ZQPZ_GPLB_MCJS_SCFXDSSGP_L 首次发型待上市股票
COMMON_SSE_ZQPZ_GPLB_MCJS_ZTSSGS_L 暂停上市股票
COMMON_SSE_ZQPZ_GPLB_MCJS_ZZSSGGJBXX_L 终止上市
Funds:
COMMON_SSE_ZQPZ_JJLB_L
Bonds:
COMMON_SSE_ZQPZ_ZQLB_GZLB_L 国债列表
COMMON_SSE_ZQPZ_ZQLB_DFZLB_L 地方债
COMMON_SSE_ZQPZ_ZQLB_GSZQYZLB_L 公司债企业债
COMMON_SSE_ZQPZ_ZQLB_KZHZLB_L 可转换债
COMMON_SSE_ZQPZ_ZQLB_ZQHGLB_L 债券回购
COMMON_SSE_ZQPZ_ZQLB_FLZLB_L 分离债列表
Derivatives:
COMMON_SSE_ZQPZ_QZ_QZLB_YDQ_L
COMMON_SSE_ZQPZ_QZ_QZLB_WDQ_L
''';
In [2]:
import json
In [3]:
import six
from six.moves.urllib.request import urlopen, Request
from six.moves.urllib.parse import urlencode
In [4]:
import pandas as pd
In [5]:
def _querysse(url, headers={}, **values):
referer = 'http://www.sse.com.cn/'
data = urlencode(values)
if six.PY3: data = data.encode('utf-8') # data should be bytes
req = Request(url, data)
req.add_header('Referer', referer)
req.headers.update(headers)
content = urlopen(req).read()
reply = json.loads(content.decode('utf-8'))
return reply['result']
In [6]:
def get_company_info(code):
"""Get details of a company by company code.
:param code: company code
"""
url = 'http://query.sse.com.cn/commonQuery.do'
reply = _querysse(url, sqlId='COMMON_SSE_ZQPZ_GP_GPLB_C', productid=code)
return reply
In [7]:
def get_stock_list(category='A'):
"""Get stock code list of a specific type
:param category: Can be A, B, IPO, Pause, or Quit, case sensitive.
"""
category_code = {'A': 'SSAG',
'B': 'SSBG',
'IPO': 'SCFXDSSGP',
'Pause': 'ZTSSGS',
'Quit': 'ZZSSGGJBXX'}
sqlid = 'COMMON_SSE_ZQPZ_GPLB_MCJS_%s_L' % category_code[category]
url = 'http://query.sse.com.cn/commonQuery.do'
reply = _querysse(url, sqlId=sqlid)
return reply
In [8]:
def get_fund_list():
sqlid = 'COMMON_SSE_ZQPZ_JJLB_L'
url = 'http://query.sse.com.cn/commonQuery.do'
reply = _querysse(url, sqlId=sqlid)
return reply
In [9]:
def get_bond_list(category='T'):
"""
"""
category_code = {'T': 'COMMON_SSE_ZQPZ_ZQLB_GZLB_L',
'M': 'COMMON_SSE_ZQPZ_ZQLB_DFZLB_L',
'C': 'COMMON_SSE_ZQPZ_ZQLB_GSZQYZLB_L',
'Tr': 'COMMON_SSE_ZQPZ_ZQLB_KZHZLB_L',
'R': 'COMMON_SSE_ZQPZ_ZQLB_ZQHGLB_L',
'S': 'COMMON_SSE_ZQPZ_ZQLB_FLZLB_L'}
sqlid = category_code[category]
url = 'http://query.sse.com.cn/commonQuery.do'
reply = _querysse(url, sqlId=sqlid)
return reply
In [10]:
def get_derivative_list():
sqlid = 'COMMON_SSE_ZQPZ_QZ_QZLB_YDQ_L'
url = 'http://query.sse.com.cn/commonQuery.do'
reply = _querysse(url, sqlId=sqlid)
return reply
In [11]:
# get_stock_list()
stocks = get_stock_list()
df = pd.DataFrame(stocks)
In [12]:
df.head()
Out[12]:
In [13]:
get_company_info(600000)
Out[13]:
In [ ]: