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