NanmiCoder/MediaCrawler · error · ValueError

Unable to parse video ID from URL: {url}

Error message

Unable to parse video ID from URL: {url}

What it means

ValueError from parse_video_info_from_url (media_platform/bilibili/help.py) when a video URL matches neither the bare 'BV...' prefix form nor the /video/BV<alphanumeric> regex. Bilibili video IDs are BV-series strings, so URLs of any other shape (av-number URLs, short links b23.tv, plain digits) are unsupported by this parser.

Source

Thrown at media_platform/bilibili/help.py:104

            - https://www.bilibili.com/video/BV1d54y1g7db
            - BV1d54y1g7db (directly pass BV number)
    Returns:
        VideoUrlInfo: Object containing video ID
    """
    # If the input is already a BV number, return directly
    if url.startswith("BV"):
        return VideoUrlInfo(video_id=url)

    # Use regex to extract BV number
    # Match /video/BV... or /video/av... format
    bv_pattern = r'/video/(BV[a-zA-Z0-9]+)'
    match = re.search(bv_pattern, url)

    if match:
        video_id = match.group(1)
        return VideoUrlInfo(video_id=video_id)

    raise ValueError(f"Unable to parse video ID from URL: {url}")


def parse_creator_info_from_url(url: str) -> CreatorUrlInfo:
    """
    Parse creator ID from Bilibili creator space URL
    Args:
        url: Bilibili creator space link
            - https://space.bilibili.com/434377496?spm_id_from=333.1007.0.0
            - https://space.bilibili.com/20813884
            - 434377496 (directly pass UID)
    Returns:
        CreatorUrlInfo: Object containing creator ID
    """
    # If the input is already a numeric ID, return directly
    if url.isdigit():
        return CreatorUrlInfo(creator_id=url)

    # Use regex to extract UID

View on GitHub (pinned to d6f7c5bb90)

Solutions

  1. Use the canonical URL form https://www.bilibili.com/video/BVxxxxxxxx or pass the bare BV id.
  2. Expand b23.tv short links first (follow redirects) and retry with the resolved URL.
  3. Convert av IDs to BV form before parsing if your source only has av numbers.
  4. Strip whitespace and validate the shape before calling.

Example fix

# before
parse_video_info_from_url('https://b23.tv/abc123')

# after
resolved = await expand_short_link('https://b23.tv/abc123')  # -> https://www.bilibili.com/video/BV1dwuKzmE26/...
parse_video_info_from_url(resolved)
Defensive patterns

Strategy: validation

Validate before calling

import re
BV_RE = re.compile(r'^(BV[a-zA-Z0-9]+)$|/video/(BV[a-zA-Z0-9]+)')
def looks_like_bilibili_video(u: str) -> bool:
    return bool(BV_RE.search(u.strip()))

Type guard

def is_parseable_bili_video(url: str) -> bool:
    url = url.strip()
    return url.startswith('BV') or bool(re.search(r'/video/BV[a-zA-Z0-9]+', url))

Try / catch

try:
    info = parse_video_info_from_url(url)
except ValueError:
    logger.warning(f'skipping unparseable bilibili url: {url}')
    continue  # batch crawl: skip, don't abort

Prevention

When it happens

Trigger: Passing 'https://www.bilibili.com/video/av1701' (av form, no BV); a b23.tv short link that needs expansion first; an empty string or a URL whose /video/ segment uses lowercase 'bv'; a 'BV' ID longer/different in charset than [a-zA-Z0-9].

Common situations: User-supplied URLs from shares/clipboard that use short links or legacy av format; data pipelines feeding historical av IDs; trailing whitespace breaking the startswith('BV') check.

Understand the failure class

Related errors


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