sansan0/TrendRadar · error · ValueError

不支持的模式: {mode}。支持的模式: daily, current

Error message

不支持的模式: {mode}。支持的模式: daily, current

What it means

RSSParser.__init__ raises ImportError when the optional dependency feedparser was not importable at module load (guarded by the HAS_FEEDPARSER flag from the try/except import at the top of parser.py). The class is constructed eagerly even though parsing of RSS/Atom requires feedparser; JSON Feed parsing does not. This is a missing-optional-dependency error, not a code bug.

Source

Thrown at mcp_server/services/data_service.py:375

        if cached:
            return cached

        # 读取今天的数据
        all_titles, id_to_name, timestamps = self.parser.read_all_titles_for_date()

        if not all_titles:
            raise DataNotFoundError(
                "未找到今天的新闻数据",
                suggestion="请确保爬虫已经运行并生成了数据"
            )

        # 根据 mode 选择要处理的标题数据
        if mode == "daily":
            titles_to_process = all_titles
        elif mode == "current":
            titles_to_process = all_titles  # 简化实现
        else:
            raise ValueError(f"不支持的模式: {mode}。支持的模式: daily, current")

        # 统计词频
        word_frequency = Counter()
        keyword_to_news = {}

        # 预加载关键词数据(避免在循环内重复调用)
        if extract_mode == "keywords":
            from trendradar.core.frequency import _word_matches
            word_groups = self.parser.parse_frequency_words()

        # 遍历要处理的标题
        for platform_id, titles in titles_to_process.items():
            for title in titles.keys():
                if extract_mode == "keywords":
                    # 基于预设关键词统计(支持正则匹配)
                    title_lower = title.lower()

                    for group in word_groups:

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Install the dependency: pip install feedparser (or reinstall trendradar with its rss extra, e.g. pip install 'trendradar[rss]' if defined).
  2. Confirm you are in the same interpreter you installed into: 'python -c "import feedparser"' with the exact python/pip pair (python -m pip install feedparser avoids pip/interpreter mismatch).
  3. Add feedparser to your deployment's requirements/lockfile so image builds include it.
  4. If RSS features are intentionally unused, guard construction: only build RSSParser when RSS monitoring is enabled in config.

Example fix

# before
parser = RSSParser()  # ImportError in envs without feedparser

# after
try:
    parser = RSSParser()
except ImportError:
    logger.warning("RSS disabled: feedparser not installed (pip install feedparser)")
    parser = None

# or simply, in shell:
# pip install feedparser
Defensive patterns

Strategy: validation

Validate before calling

from trendradar.crawler.rss import parser as rss_parser_mod

def rss_available() -> bool:
    return rss_parser_mod.HAS_FEEDPARSER

if not rss_available():
    logger.warning("RSS features disabled; pip install feedparser")

Type guard

from trendradar.crawler.rss.parser import RSSParser

def can_use_rss() -> bool:
    """Cheap check: feedparser importable == RSSParser constructible."""
    try:
        import feedparser  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    parser = RSSParser()
except ImportError as e:
    logger.error("RSS disabled: %s", e)
    parser = None
# gate all later use: if parser is None: skip rss jobs

Prevention

When it happens

Trigger: Instantiating RSSParser() in an environment where 'import feedparser' failed: trendradar installed without the rss extra (pip install trendradar vs pip install trendradar[rss]), a venv mismatch where the package was installed into a different interpreter, or a broken/partial feedparser install.

Common situations: Deploying to a slim Docker image or CI runner without the RSS extras; activating the wrong virtualenv/conda env; a requirements.txt that omits feedparser because it is optional; upgrading Python versions and losing site-packages.

Related errors


AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15). Data as JSON: /api/errors/7868cd71f5c5ca65. Report an issue: GitHub.