D4Vinci/Scrapling · error · ValueError

Could not parse cookies '{cookies}': {err}

Error message

Could not parse cookies '{cookies}': {err}

What it means

When the CLI's cookie option is used, each parsed key/value pair is inserted into the cookie dict inside a try/except; any failure while assigning a cookie re-raised as ValueError with the offending cookie string. In practice this wraps parser failures from _CookieParser, so it means the cookie string could not be interpreted as cookies.

Source

Thrown at scrapling/cli.py:75

    if ai_targeted:
        kwargs.setdefault("block_ads", True)
    response = fetcher_func(url, **kwargs)
    Convertor.write_content_to_file(response, str(output_path), css_selector, main_content_only=ai_targeted)
    log.info(f"Content successfully saved to '{output_path}'")


def __ParseExtractArguments(
    headers: List[str], cookies: str, params: str, json: Optional[str] = None
) -> Tuple[Dict[str, str], Dict[str, str], Dict[str, str], Optional[Dict[str, str]]]:
    """Parse arguments for extract command"""
    parsed_headers, parsed_cookies = _ParseHeaders(headers)
    if cookies:
        for key, value in _CookieParser(cookies):
            try:
                parsed_cookies[key] = value
            except Exception as err:
                raise ValueError(f"Could not parse cookies '{cookies}': {err}")

    parsed_json = __ParseJSONData(json)
    parsed_params = {}
    for param in params:
        if "=" in param:
            key, value = param.split("=", 1)
            parsed_params[key] = value

    return parsed_headers, parsed_cookies, parsed_params, parsed_json


def __BuildRequest(headers: List[str], cookies: str, params: str, json: Optional[str] = None, **kwargs) -> Dict:
    """Build a request object using the specified arguments"""
    # Parse parameters
    parsed_headers, parsed_cookies, parsed_params, parsed_json = __ParseExtractArguments(headers, cookies, params, json)
    # Build request arguments
    request_kwargs: Dict[str, Any] = {
        "headers": parsed_headers if parsed_headers else None,

View on GitHub (pinned to 5d213a2d47)

Solutions

  1. Use plain 'key=value' pairs separated by ';', e.g. --cookies 'session=abc123; token=xyz'
  2. Strip Set-Cookie-only attributes (Path, Domain, Expires, HttpOnly, Secure) before passing
  3. Check shell quoting: the whole cookie string should be one argument
  4. If the header is complex, pass cookies via the headers option as a Cookie header instead

Example fix

# before
scrapling fetch https://example.com --cookies "session=abc; Path=/; HttpOnly"
# ValueError: Could not parse cookies ...

# after
scrapling fetch https://example.com --cookies 'session=abc'
Defensive patterns

Strategy: validation

Validate before calling

def normalize_cookies(cookie_str: str) -> str:
    skip = {'path', 'domain', 'expires', 'max-age', 'httponly', 'secure', 'samesite'}
    pairs = [p.strip() for p in cookie_str.split(';') if '=' in p]
    keep = [p for p in pairs if p.split('=', 1)[0].strip().lower() not in skip]
    return '; '.join(keep)

cookies = normalize_cookies('session=abc; Path=/; HttpOnly')  # 'session=abc'

Prevention

When it happens

Trigger: Passing a malformed cookie string to `scrapling fetch URL --cookies ...`, e.g. missing '=' separator, stray quotes, or a whole Set-Cookie header (with attributes like Path=/; HttpOnly) pasted where a simple 'k=v; k2=v2' string is expected.

Common situations: Copying a full Set-Cookie header from DevTools instead of the Cookie request header, unterminated quoting in shell scripts, or separators other than ';' between pairs.

Related errors


AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14). Data as JSON: /api/errors/793c9da218a14831. Report an issue: GitHub.