tushare 의 gettoday_all 복구 인터페이스 전체 코드
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @license : (C) Copyright 2017-2020.
# @contact : xsophiax
# @Time : 2020/6/8 10:10
# @File : get_today_all_xsophiax.py
# @Software: PyCharm
# @desc :
import time
import json
import lxml.html
from lxml import etree
import pandas as pd
import numpy as np
import datetime
from tushare.stock import cons as ct
import re
from tushare.util import dateu as du
from tushare.util.formula import MA
import os
from tushare.util.conns import get_apis, close_apis
from tushare.stock.fundamental import get_stock_basics
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
v = pd.__version__
if int(v.split('.')[1])>=25 or int(v.split('.')[0])>0:
from io import StringIO
else:
from pandas.compat import StringIO
def _parsing_dayprice_json(types=None, page=1):
"""
, json
Parameters
------
pageNum:
return
-------
DataFrame (DataFrame)
"""
ct._write_console()
request = Request(ct.SINA_DAY_PRICE_URL%(ct.P_TYPE['http'], ct.DOMAINS['vsf'],
ct.PAGES['jv'], types, page))
text = urlopen(request, timeout=10).read()
if text == 'null':
return None
reg = re.compile(r'\,(.*?)\:')
text = reg.sub(r',"\1":', text.decode('gbk') if ct.PY3 else text)
text = text.replace('"{"symbol', '{"symbol')
text = text.replace('{symbol', '{"symbol"')
text = text.replace('""', '"')
if ct.PY3:
jstr = json.dumps(text)
else:
jstr = json.dumps(text, encoding='GBK')
js = json.loads(jstr)
df = pd.DataFrame(pd.read_json(js, dtype={'code':object}),
columns=ct.DAY_TRADING_COLUMNS)
df = df.drop('symbol', axis=1)
# df = df.ix[df.volume > 0]
return df
def get_today_all():
"""
return
-------
DataFrame
: , , , , , , , , , , , , , ,
"""
ct._write_head()
df = _parsing_dayprice_json('hs_a', 1)
if df is not None:
for i in range(2, ct.PAGE_NUM[1]):
newdf = _parsing_dayprice_json('hs_a', i)
if newdf.shape[0] > 0:
df = df.append(newdf, ignore_index=True)
else:
break
df = df.append(_parsing_dayprice_json('shfxjs', 1),
ignore_index=True)
return df
테스트 인터페이스
if __name__ == '__main__':
stk_data = get_today_all()
print(stk_data.head()) 출력 결과
code name changepercent ... pb mktcap nmc
0 688598 0.098 ... 17.724 7.359200e+05 168195.895850
1 688588 -1.749 ... 23.838 1.888047e+06 170577.637600
2 688566 0.645 ... 12.974 8.464716e+05 174157.439296
3 688516 0.000 ... 9.014 5.541307e+05 113557.536144
4 688466 0.147 ... 9.280 4.195691e+05 95511.512172