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.
 
 
 
 
 

75 lines
2.3 KiB

#!/usr/bin/env python3
"""
查询中国股市 - v2
"""
import urllib.request
import socket
import json
# 设置超时
socket.setdefaulttimeout(10)
print("正在查询中国股市...")
# 尝试腾讯财经接口
try:
# 上证指数
url = "http://qt.gtimg.cn/q=sh000001"
print(f"正在访问: {url}")
with urllib.request.urlopen(url) as f:
content = f.read().decode('gbk')
print(f"\n✅ 获取成功!")
# 解析腾讯格式
# v_sh000001="1~上证指数~000001~3123.45~...";
if 'v_sh000001' in content:
data_part = content.split('"')[1]
parts = data_part.split('~')
name = parts[1]
code = parts[2]
current = parts[3]
prev_close = parts[4]
open_price = parts[5]
volume = parts[6]
amount = parts[7]
high = parts[33]
low = parts[34]
print(f"\n📊 {name} ({code})")
print(f" 当前: {current}")
print(f" 今开: {open_price}")
print(f" 昨收: {prev_close}")
print(f" 最高: {high}")
print(f" 最低: {low}")
print(f" 成交量: {volume}")
print(f" 成交额: {amount}")
# 计算涨跌幅
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尝试其他方式...")
# 试试最简单的方式,直接告诉用户现在的时间
from datetime import datetime
now = datetime.now()
print(f"\n📅 当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
print("💡 建议: 您可以直接访问东方财富网、新浪财经等查看股市")
print(" 或告诉我您想关注哪只股票,我再想想其他办法")