D4Vinci/Scrapling · error · ValueError

Could not parse cookie string from header '{header_value}':

Error message

Could not parse cookie string from header '{header_value}': {e}

What it means

Raised by `_ParseHeaders` in scrapling/core/utils/_shell.py:42 when a `Cookie:` header line is found and cookie parsing is enabled (the default). The header's value is fed through `_CookieParser` (an `http.cookies.SimpleCookie` derivative); if that parser throws — typically malformed cookie syntax that even the lenient parser rejects — the original exception is wrapped in a ValueError showing the cookie string. Marked `# pragma: no cover`, so it is a defensive path for badly malformed cookie strings.

Source

Thrown at scrapling/core/utils/_shell.py:42

    for header_line in header_lines:
        if ":" not in header_line:
            if header_line.endswith(";"):
                header_key = header_line[:-1].strip()
                header_value = ""
                header_dict[header_key] = header_value
            else:
                raise ValueError(f"Could not parse header without colon: '{header_line}'.")
        else:
            header_key, header_value = header_line.split(":", 1)
            header_key = header_key.strip()
            header_value = header_value.strip()

            if parse_cookies:
                if header_key.lower() == "cookie":
                    try:
                        cookie_dict = {key: value for key, value in _CookieParser(header_value)}
                    except Exception as e:  # pragma: no cover
                        raise ValueError(f"Could not parse cookie string from header '{header_value}': {e}")
                else:
                    header_dict[header_key] = header_value
            else:
                header_dict[header_key] = header_value

    return header_dict, cookie_dict

View on GitHub (pinned to 5d213a2d47)

Solutions

  1. Fix the cookie string to standard `name=value; name2=value2` syntax and single-quote the whole `-H` argument in the shell.
  2. URL-encode exotic characters in cookie values or drop cookies that aren't needed.
  3. If you don't need cookie parsing, pass headers through a path with `parse_cookies=False` (e.g. `--extra-headers` handling in the CLI) or use the `-b/--cookie` style option per cookie.
  4. Test the string in Python first: `dict(http.cookies.SimpleCookie())` behavior with `_CookieParser` to see the underlying error.

Example fix

# before
scrapling shell -H 'Cookie: session=abc"; ; bad==' https://example.com

# after
scrapling shell -H 'Cookie: session=abc; theme=dark' https://example.com
Defensive patterns

Strategy: validation

Validate before calling

from http.cookies import SimpleCookie

def cookie_string_ok(cookie_value: str) -> bool:
    try:
        c = SimpleCookie()
        c.load(cookie_value)
        return True
    except Exception:
        return False

Try / catch

try:
    headers, cookies = _ParseHeaders(lines)
except ValueError as e:
    if 'cookie' in str(e).lower():
        headers, _ = _ParseHeaders([l for l in lines if not l.lower().startswith('cookie')])
        # re-add a cleaned cookie header manually
    else:
        raise

Prevention

When it happens

Trigger: Passing `-H 'Cookie: session=abc; ; ;'` or a cookie value with illegal characters/unbalanced quotes that `SimpleCookie` cannot parse, while `parse_cookies=True` (default for `scrapling shell`/CLI fetch). `_ParseHeaders(lines, parse_cookies=False)` (used by `extra_headers` paths in cli.py) never raises this.

Common situations: Pasting a Cookie header copied from DevTools where encoding got mangled (e.g. quotes stripped by the shell); cookie values containing `"` or control characters; hand-typing a cookie string with stray separators.

Related errors


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