前言
新浪的API不知何时失效了,只好另寻出路。
于是找到了雪球。
基本信息
API示例:
URL:
https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol=SH601231,SZ002299&_=1541640828575
带上UA信息发送请求
返回:
{
"data": [
{
"symbol": "SZ002299",
"current": 18.58,
"percent": 0.7,
"chg": 0.13,
"timestamp": 1647587043000,
"volume": 5477309,
"amount": 1.01428422E8,
"market_capital": 2.3113025698E10,
"float_market_capital": 2.2825834396E10,
"turnover_rate": 0.45,
"amplitude": 1.9,
"open": 18.49,
"last_close": 18.45,
"high": 18.67,
"low": 18.32,
"avg_price": 18.518,
"trade_volume": 0,
"side": -1,
"is_trade": false,
"level": 1,
"trade_session": null,
"trade_type": null,
"current_year_percent": -23.13,
"trade_unique_id": "5477309",
"type": 11,
"bid_appl_seq_num": null,
"offer_appl_seq_num": null,
"volume_ext": null,
"traded_amount_ext": null,
"trade_type_v2": null
},
{
"symbol": "SH601231",
"current": 12.52,
"percent": -0.32,
"chg": -0.04,
"timestamp": 1647586800000,
"volume": 4410579,
"amount": 5.511005391E7,
"market_capital": 2.7674063807E10,
"float_market_capital": 2.7349295358E10,
"turnover_rate": 0.2,
"amplitude": 2.95,
"open": 12.49,
"last_close": 12.56,
"high": 12.59,
"low": 12.22,
"avg_price": 12.494970367836059,
"trade_volume": null,
"side": null,
"is_trade": false,
"level": 2,
"trade_session": null,
"trade_type": null,
"current_year_percent": -22.04,
"trade_unique_id": null,
"type": 11,
"bid_appl_seq_num": null,
"offer_appl_seq_num": null,
"volume_ext": null,
"traded_amount_ext": null,
"trade_type_v2": null
}
],
"error_code": 0,
"error_description": null
}
百度翻译结果:
{
"符号":"SH601231",
"当前":12.52,
"百分比":-0.32,
"chg":-0.04,
"时间戳":1647586800000,
"卷":4410579,
"金额":5.511005391E7,
"市场资本":2.7674063807E10,
"浮动市场资本":2.7349295358E10,
"周转率":0.2,
"振幅":2.95,
"开放":12.49,
"最后一关":12.56,
"高":12.59,
"低":12.22,
"平均价格":12.494970367836059,
"交易量":空,
"边":空,
"is_trade":错误,
"级别":2,
"交易会话":空,
"交易类型":空,
"当前年百分比":-22.04,
"trade_unique_id":空,
"类型":11,
"bid_appl_seq_num":空,
"offer_appl_seq_num":空,
"卷外部":空,
"交易金额ext":空,
"交易类型2":空
}
小结
和新浪相比,不含股票名了。
和新浪相比,使用的是JSON数据,更方便处理了。
代码计划
只需要当前价格、最高最低、昨天开盘、百分比、涨幅
因为雪球API不带股票名,后续需要股票名的需要应用需要自己先找好放在字典中了
示例代码
import requests,json
# 用来给股票代码添加前缀,已废弃
def add_exchange(shares):
shares_str = str(shares)
if (shares_str[:2] == "00") or (shares_str[:3] == "200") or (shares_str[:3] == "300"):
r_data = "sz"+shares_str
elif (shares_str[:2] == "60") or (shares_str[:2] == "51") or (shares_str[:3] == "900") or (shares_str[:2] == "68"):
r_data = "sh"+shares_str
else:
r_data = ""
return r_data
# 处理返回的数据
def xueqiu_analysis_return(dat):
jdat = json.loads(dat)
rdat = []
for n in jdat['data']:
ndic = {}
# 股票代码
ndic['code'] = n['symbol']
# 现价
ndic['current'] = n['current']
# 涨幅百分比
ndic['percent'] = n['percent']
# 涨幅
ndic['chg'] = n['chg']
# 最高价
ndic['high'] = n['high']
# 最低价
ndic['low'] = n['low']
# 今日开盘
ndic['open'] = n['open']
# 昨日收盘
ndic['last_close'] = n['last_close']
rdat.append(ndic)
return rdat
def xueqiu_api(shares_lis):
he = {
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62"
}
url_parameter = ""
# 把股票代码拼起来,注意需要大写
for shares in shares_lis:
exchange_shares = shares["code"].upper()
if exchange_shares != "":
url_parameter = url_parameter + exchange_shares + ","
# 发送API请求
url = "https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol="+url_parameter[:-1]
r = requests.get(url,headers=he)
r_dat = r.text
return r_dat
# 持仓情况列表
shares_list = [
{'code': 'SZ002299', 'price': '25.477', 'quantity': '1200'},
{'code': 'SH513050', 'price': '1.853', 'quantity': '8100'},
{'code': 'SH601231', 'price': '14.372', 'quantity': '800'},
{'code': 'SZ002273', 'price': '8.434', 'quantity': '600'},
{'code': 'SZ002461', 'price': '9.208', 'quantity': '800'}
]
for n in xueqiu_analysis_return(xueqiu_api(shares_list)):
print(n)
运行结果
{'code': 'SZ002299', 'current': 18.58, 'percent': 0.7, 'chg': 0.13, 'high': 18.67, 'low': 18.32, 'open': 18.49, 'last_close': 18.45}
{'code': 'SZ002461', 'current': 7.51, 'percent': 3.16, 'chg': 0.23, 'high': 7.64, 'low': 7.21, 'open': 7.28, 'last_close': 7.28}
{'code': 'SH513050', 'current': 1.022, 'percent': 0.0, 'chg': 0.0, 'high': 1.043, 'low': 0.991, 'open': 0.996, 'last_close': 1.022}
{'code': 'SZ002273', 'current': 11.53, 'percent': 0.09, 'chg': 0.01, 'high': 11.65, 'low': 11.4, 'open': 11.49, 'last_close': 11.52}
{'code': 'SH601231', 'current': 12.52, 'percent': -0.32, 'chg': -0.04, 'high': 12.59, 'low': 12.22, 'open': 12.49, 'last_close': 12.56}
使用场景
结合智能家具系统HomeAssistant,在APP/WEB端显示。
或者使用HomeAssistant与设备联动,实现涨幅/价格达到某个阈值控制家中设备。
先挖个坑,下次填
结合微信机器人,随时查看盈利状况。
或者在涨幅/价格达到某个阈值发送信息提醒。
再挖个坑,下次填
最后放个博客:https://www.9kr.cc/
全部评论