NanmiCoder/MediaCrawler · error · ValueError

[XhsStoreFactory.create_store] Invalid save option only supp

Error message

[XhsStoreFactory.create_store] Invalid save option only supported csv or db or json or sqlite or mongodb or excel ...

What it means

Raised by XhsStoreFactory.create_store() when config.SAVE_DATA_OPTION is not a key in the xhs STORES registry (csv, db, postgres, json, jsonl, sqlite, mongodb, excel). Fail-fast at first store creation during a xiaohongshu crawl.

Source

Thrown at store/xhs/__init__.py:50


class XhsStoreFactory:
    STORES = {
        "csv": XhsCsvStoreImplement,
        "db": XhsDbStoreImplement,
        "postgres": XhsDbStoreImplement,
        "json": XhsJsonStoreImplement,
        "jsonl": XhsJsonlStoreImplement,
        "sqlite": XhsSqliteStoreImplement,
        "mongodb": XhsMongoStoreImplement,
        "excel": XhsExcelStoreImplement,
    }

    @staticmethod
    def create_store() -> AbstractStore:
        store_class = XhsStoreFactory.STORES.get(config.SAVE_DATA_OPTION)
        if not store_class:
            raise ValueError("[XhsStoreFactory.create_store] Invalid save option only supported csv or db or json or sqlite or mongodb or excel ...")
        return store_class()


def get_video_url_arr(note_item: Dict) -> List:
    """
    Get video url array
    Args:
        note_item:

    Returns:

    """
    if note_item.get('type') != 'video':
        return []

    video_dict = note_item.get('video')
    if not video_dict:
        return []

View on GitHub (pinned to d6f7c5bb90)

Solutions

  1. Set SAVE_DATA_OPTION to exactly: csv, db, postgres, json, jsonl, sqlite, mongodb, or excel
  2. 'json' is the default and needs no external services — start there for a first run
  3. If you intended MySQL, the key is 'db' plus the MySQL env/connection config
  4. Double-check no env var (SAVE_DATA_OPTION) overrides the file value

Example fix

// before
SAVE_DATA_OPTION = "mongo"

// after
SAVE_DATA_OPTION = "mongodb"
Defensive patterns

Strategy: validation

Validate before calling

from store.xhs import XhsStoreFactory

if config.SAVE_DATA_OPTION not in XhsStoreFactory.STORES:
    raise SystemExit(
        f"SAVE_DATA_OPTION={config.SAVE_DATA_OPTION!r} invalid; "
        f"choose from {sorted(XhsStoreFactory.STORES)}"
    )

Type guard

def is_valid_save_option(opt: str) -> bool:
    return isinstance(opt, str) and opt in XhsStoreFactory.STORES

Try / catch

try:
    store = XhsStoreFactory.create_store()
except ValueError as e:
    raise SystemExit(f"fix config before crawling: {e}") from e

Prevention

When it happens

Trigger: SAVE_DATA_OPTION set to an unregistered value — typo, wrong case ('Excel'), or a backend name from other tooling — when the xhs crawler calls create_store() to persist notes/comments.

Common situations: Most common first-run failure for xhs users who edited config/base_config.py; switching from json to a database without knowing the exact key; env var override with a different value than the file.

Related errors


AI-assisted analysis of NanmiCoder/MediaCrawler@d6f7c5bb90 (2026-08-15). Data as JSON: /api/errors/02b2ef8a5e8b8d61. Report an issue: GitHub.