You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
#!/usr/bin/env python3
|
|
"""
|
|
查询中国股市
|
|
"""
|
|
|
|
import urllib.request
|
|
import socket
|
|
import json
|
|
|
|
# 设置超时
|
|
socket.setdefaulttimeout(10)
|
|
|
|
print("正在查询中国股市...")
|
|
|
|
# 尝试新浪财经的接口
|
|
try:
|
|
# 上证指数 000001.SS
|
|
url = "https://hq.sinajs.cn/list=sh000001"
|
|
print(f"正在访问: {url}")
|
|
|
|
with urllib.request.urlopen(url) as f:
|
|
content = f.read().decode('gbk')
|
|
print(f"\n✅ 获取成功!")
|
|
|
|
# 解析新浪格式
|
|
# var hq_str_sh000001="上证指数,3123.45,3100.00,...";
|
|
if 'hq_str_sh000001' in content:
|
|
data = content.split('"')[1]
|
|
parts = data.split(',')
|
|
|
|
name = parts[0]
|
|
open_price = parts[1]
|
|
prev_close = parts[2]
|
|
current = parts[3]
|
|
high = parts[4]
|
|
low = parts[5]
|
|
|
|
print(f"\n📊 {name} (上证指数)")
|
|
print(f" 当前: {current}")
|
|
print(f" 今开: {open_price}")
|
|
print(f" 昨收: {prev_close}")
|
|
print(f" 最高: {high}")
|
|
print(f" 最低: {low}")
|
|
|
|
# 计算涨跌幅
|
|
try:
|
|
curr = float(current)
|
|
prev = float(prev_close)
|
|
change = curr - prev
|
|
pct = (change / prev) * 100
|
|
|
|
if change > 0:
|
|
print(f" 📈 涨跌: +{change:.2f} (+{pct:.2f}%)")
|
|
elif change < 0:
|
|
print(f" 📉 涨跌: {change:.2f} ({pct:.2f}%)")
|
|
else:
|
|
print(f" 涨跌: {change:.2f} (0.00%)")
|
|
except:
|
|
pass
|
|
|
|
except Exception as e:
|
|
print(f"❌ 查询失败: {e}")
|
|
print("\n尝试其他接口...")
|
|
|