From 4fde6385a5b1bef8e758a729414347153dd81708 Mon Sep 17 00:00:00 2001 From: Hiller Liao Date: Sat, 29 Mar 2025 16:05:32 +0800 Subject: [PATCH] refactor(nasdaq): remove caching and improve error handling for symbol change history --- requirements.txt | 1 + rsshub/spiders/nasdaq/symbol_change.py | 29 +++++--------------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1cf4c8d..1472d4e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ icecream==2.1.1 idna==3.3 itsdangerous==2.0.1 Jinja2==3.0.3 +lxml==4.6.3 MarkupSafe==2.0.1 parsel==1.6.0 pyppeteer==1.0.2 diff --git a/rsshub/spiders/nasdaq/symbol_change.py b/rsshub/spiders/nasdaq/symbol_change.py index 34a43ea..e0f1133 100644 --- a/rsshub/spiders/nasdaq/symbol_change.py +++ b/rsshub/spiders/nasdaq/symbol_change.py @@ -1,13 +1,9 @@ import json import requests from rsshub.utils import DEFAULT_HEADERS -from cachetools import TTLCache domain = 'https://www.nasdaq.com' -# 缓存数据,设置缓存大小为 1,TTL 为 300 秒(5 分钟) -cache = TTLCache(maxsize=1, ttl=300) - def parse(post): item = {} @@ -18,35 +14,20 @@ def parse(post): def ctx(category=''): - if 'cached_data' in cache: - return cache['cached_data'] # 返回缓存数据 - url = 'https://api.nasdaq.com/api/quote/list-type-extended/symbolchangehistory' DEFAULT_HEADERS.update({ 'Referer': 'https://www.nasdaq.com/market-activity/stocks/symbol-change-history', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' }) - try: - response = requests.get(url, headers=DEFAULT_HEADERS, timeout=10) # 设置超时时间为 10 秒 - response.raise_for_status() - posts = json.loads(response.text)['data']['symbolChangeHistoryTable']['rows'] - except requests.exceptions.RequestException as e: - return { - 'title': 'Stock Symbol Change History - Nasdaq', - 'link': 'https://www.nasdaq.com/market-activity/stocks/symbol-change-history', - 'description': f'Error fetching data: {e}', - 'author': 'hillerliao', - 'items': [], - } - - result = { + response = requests.get(url, headers=DEFAULT_HEADERS) + response.raise_for_status() # 确保请求成功 + posts = json.loads(response.text)['data']['symbolChangeHistoryTable']['rows'] + + return { 'title': 'Stock Symbol Change History - Nasdaq', 'link': 'https://www.nasdaq.com/market-activity/stocks/symbol-change-history', 'description': 'View the history of stock symbol changes on Nasdaq. Stay informed on corporate actions, mergers, and rebrandings that result in symbol updates', 'author': 'hillerliao', 'items': list(map(parse, posts)), } - - cache['cached_data'] = result # 缓存结果 - return result