tushare 之get_today_all修复接口完整code

#!/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

 

你可能感兴趣的:(tushare,技术内幕,量化投资)