fix(stock): SZA公告接口+行业新闻回退到指数代理
This commit is contained in:
parent
24337bf672
commit
dec34bf88a
|
|
@ -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=""):
|
||||
|
|
|
|||
Loading…
Reference in New Issue