From dec34bf88a0b0dc433f42a31dbcbcd85e2c256ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF=20A06?= Date: Sun, 12 Jul 2026 09:41:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(stock):=20SZA=E5=85=AC=E5=91=8A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3+=E8=A1=8C=E4=B8=9A=E6=96=B0=E9=97=BB=E5=9B=9E?= =?UTF-8?q?=E9=80=80=E5=88=B0=E6=8C=87=E6=95=B0=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/stock_selector.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/scripts/stock_selector.py b/scripts/stock_selector.py index a35477b0..28b3e778 100644 --- a/scripts/stock_selector.py +++ b/scripts/stock_selector.py @@ -238,7 +238,7 @@ def get_technical_score(code): def get_stock_news(code, name, max_news=3): """获取个股最新新闻(东方财富)""" try: - url = f"https://np-anotice-stock.eastmoney.com/api/security/ann?sr=-1&page_size={max_news}&page_index=1&ann_type=SHS&stock_list={code}" + url = f"https://np-anotice-stock.eastmoney.com/api/security/ann?sr=-1&page_size={max_news}&page_index=1&ann_type=SZA&stock_list={code}" import subprocess, shlex, os, json env = dict(os.environ) for k in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]: @@ -259,11 +259,11 @@ def get_stock_news(code, name, max_news=3): def get_sector_news(industry, max_news=2): - """获取行业相关新闻(东方财富搜索)""" - keyword_map = {"白酒": "白酒", "银行": "银行股", "保险": "保险", "新能源": "新能源"} - keyword = keyword_map.get(industry, industry) or industry or "" + """获取行业相关新闻 — 回退到指数情绪代理(行业API不可用时)""" try: - import subprocess, shlex, os, json, re + # 先尝试东财快讯 + keyword_map = {"白酒": "白酒", "银行": "银行", "保险": "保险", "新能源": "新能源"} + keyword = keyword_map.get(industry, industry) or industry or "" base = "https://search-api-web.eastmoney.com/search/jsonp" param = ('{"uid":"","keyword":"' + keyword + '","type":["cmsArticleListNew"],' '"client":"web","clientVersion":"curr","clientType":"web",' @@ -272,17 +272,37 @@ def get_sector_news(industry, max_news=2): env = dict(os.environ) for k in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]: env.pop(k, None) - cmd = ["curl", "-s", "--max-time", "6", "--compressed", url] + cmd = ["curl", "-s", "--max-time", "6", + "-H", "Referer: https://so.eastmoney.com/", + "-H", "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/120 Safari/537.36", + url] r = subprocess.run(cmd, capture_output=True, timeout=8, env=env) raw = r.stdout.decode("utf-8", errors="ignore") m = re.search(r'\(\[.*\]\)', raw, re.DOTALL) if m: items = json.loads(m.group(1)) news = [f"· {it.get('title','')[:40]}" for it in items[:max_news] if it.get('title')] - return news if news else ["无相关行业新闻"] - return ["新闻解析失败"] + if news: + return news except Exception: - return ["行业新闻获取失败"] + pass + # 回退:使用指数涨跌作为行业情绪代理 + try: + sector_codes = {"白酒": "sh000858", "银行": "sh000001", "保险": "sh601318", "新能源": "sz399808"} + code = sector_codes.get(industry, "sh000001") + mc = code[:2] + code[2:] + url = f"https://qt.gtimg.cn/q={mc}" + text = get_url_gbk(url) + parts = text.split("~") + if len(parts) > 5: + p = float(parts[3]) + y = float(parts[4]) + chg = (p-y)/y*100 + sentiment = "📈 行业指数上涨" if chg > 0 else "📉 行业指数下跌" + return [f"· {sentiment} ({chg:+.2f}%) — {parts[1]}"] + except Exception: + pass + return ["· 行业新闻获取失败(回退到静态规则)"] def get_sentiment_score_from_news(code, name="", industry=""):