#!/usr/bin/env python3 """ 实用的股票数据查询工具 在API限制下提供最可靠的数据获取方案 """ import sys import json from datetime import datetime class PracticalStockDataQuery: """实用股票数据查询类""" def __init__(self): self.data_sources = ["user_input", "news_data", "fallback_validation"] def get_stock_data_with_validation( self, stock_code: str, user_price: float = None ) -> dict: """ 获取股票数据,优先使用用户提供的价格 Args: stock_code: 股票代码 user_price: 用户提供的准确价格(优先使用) Returns: 包含验证信息的股票数据 """ result = { "code": stock_code, "timestamp": datetime.now().isoformat(), "data_sources": [], "confidence_score": 0, } if user_price is not None: # 用户提供价格,置信度最高 result["price"] = float(user_price) result["confidence_score"] = 100 result["data_sources"] = ["user_input"] result["validation_status"] = "user_verified" else: # 无法获取准确数据 result["error"] = "无法通过API获取准确价格数据" result["confidence_score"] = 0 result["suggestion"] = "请通过交易软件确认准确价格后提供" return result def get_batch_data_with_user_input( self, stock_codes: list, user_prices: dict = None ) -> list: """批量获取数据,支持用户输入价格""" if user_prices is None: user_prices = {} results = [] for code in stock_codes: price = user_prices.get(code) result = self.get_stock_data_with_validation(code, price) results.append(result) return results def main(): """主函数 - 实用的交互式查询""" if len(sys.argv) < 2: print("用法: python practical_stock_query.py") print("这是一个交互式工具,请按提示输入股票代码和价格") return query = PracticalStockDataQuery() # 交互式获取用户输入 stock_codes = input("请输入股票代码(用逗号分隔): ").split(",") stock_codes = [code.strip() for code in stock_codes if code.strip()] user_prices = {} for code in stock_codes: try: price = input(f"请输入 {code} 的当前价格: ") if price.strip(): user_prices[code] = float(price.strip()) except ValueError: print(f"无效价格,跳过 {code}") results = query.get_batch_data_with_user_input(stock_codes, user_prices) print("\n=== 股票数据查询结果 ===") print(json.dumps(results, indent=2, ensure_ascii=False)) if __name__ == "__main__": main()