From 76e86f53c970066cd8fffd9947284a0aaba76f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF=20A06?= Date: Sun, 12 Jul 2026 09:58:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(stock=5Fnews):=20get=5Furl=E6=94=B9curl?= =?UTF-8?q?=E5=AD=90=E8=BF=9B=E7=A8=8B+=E5=8E=9F=E6=B2=B9=E8=A7=A3?= =?UTF-8?q?=E6=9E=90hf=5FOIL=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/stock_news.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/scripts/stock_news.py b/scripts/stock_news.py index 59fe3e97..fc02f716 100644 --- a/scripts/stock_news.py +++ b/scripts/stock_news.py @@ -30,10 +30,15 @@ def send_feishu(msg): def get_url(url, timeout=4, enc="gbk"): - """快速HTTP GET,统一处理编码""" + """快速HTTP GET,统一用curl避免urllib在hermes/scripts目录异常""" + import subprocess, shlex, os + env = dict(os.environ) + for k in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]: + env.pop(k, None) + cmd = ["curl", "-s", "--max-time", str(timeout), url] try: - data = urllib.request.urlopen(url, timeout=timeout).read() - return data.decode(enc, errors="ignore") + r = subprocess.run(cmd, capture_output=True, timeout=timeout + 2, env=env) + return r.stdout.decode(enc, errors="ignore") except Exception: return "" @@ -60,9 +65,15 @@ def get_oil(): try: url = "https://qt.gtimg.cn/q=hf_OIL" text = get_url(url) - parts = text.split("~") - if len(parts) > 10 and parts[3]: - return {"value": float(parts[3]), "change": float(parts[3]) - float(parts[4])} + # hf_OIL 格式: v_hf_OIL="75.23,-1.40,75.22,75.27,77.52,75.22,05:59:59,76.30,76.15,0,77,2,2026-07-11,文字" + # parts[0]="v_hf_OIL=", parts[1]="75.23,-1.40,..." + if "=" in text: + val_part = text.split("=", 1)[1].strip('"; \n') + vals = val_part.split(",") + if len(vals) >= 8: + price = float(vals[0]) + yclose = float(vals[7]) + return {"value": price, "change": price - yclose} except Exception: pass return None