{"record":{"id":"b35342a63a504099","repo":"NanmiCoder/MediaCrawler","slug":"d-c0-not-found-in-cookies","errorCode":null,"errorMessage":"d_c0 not found in cookies","messagePattern":"d_c0 not found in cookies","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"media_platform/zhihu/client.py","lineNumber":78,"sourceCode":"        self.timeout = timeout\n        self.default_headers = headers\n        self.cookie_urls = [\"https://www.zhihu.com\"]\n        self.cookie_dict = cookie_dict\n        self._extractor = ZhihuExtractor()\n        # Initialize proxy pool (from ProxyRefreshMixin)\n        self.init_proxy_pool(proxy_ip_pool)\n\n    async def _pre_headers(self, url: str) -> Dict:\n        \"\"\"\n        Sign request headers\n        Args:\n            url: Request URL with query parameters\n        Returns:\n\n        \"\"\"\n        d_c0 = self.cookie_dict.get(\"d_c0\")\n        if not d_c0:\n            raise Exception(\"d_c0 not found in cookies\")\n        sign_res = sign(url, self.default_headers[\"cookie\"])\n        headers = self.default_headers.copy()\n        headers['x-zst-81'] = sign_res[\"x-zst-81\"]\n        headers['x-zse-96'] = sign_res[\"x-zse-96\"]\n        return headers\n\n    @retry(stop=stop_after_attempt(3), wait=wait_fixed(1))\n    async def request(self, method, url, **kwargs) -> Union[str, Any]:\n        \"\"\"\n        Wrapper for httpx common request method with response handling\n        Args:\n            method: Request method\n            url: Request URL\n            **kwargs: Other request parameters such as headers, body, etc.\n\n        Returns:\n\n        \"\"\"","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/NanmiCoder/MediaCrawler/blob/d6f7c5bb906b6dac40ddf343ef9e26438a3de092/media_platform/zhihu/client.py#L60-L96","documentation":"Bare Exception raised by ZhiHuClient._pre_headers when the d_c0 cookie is absent from the session cookie dict. d_c0 is zhihu's device credential; it is a required input to the x-zse-96/x-zst-81 request signing, so without it no signed API call can be built.","triggerScenarios":"Calling any signed zhihu request (get/post wrappers) after login cookies were loaded but no d_c0 value was present - e.g. cookie string exported before visiting zhihu, a cookie from a logged-out session, or a truncated/malformed ZHIHU_COOKIES config value.","commonSituations":"Cookie string pasted incompletely so the d_c0 entry got cut off; using a fresh browser profile where zhihu never set d_c0; cookie parsed with the wrong key separator so cookie_dict keys are wrong.","solutions":["Visit zhihu.com in a browser, confirm d_c0 exists in devtools Application > Cookies, and re-export the full cookie string into config.","If login type is cookie, ensure the exported string includes d_c0=...; if using qrcode login, wait for the session to fully establish before crawling.","Log the cookie_dict keys (names only) before crawling to verify d_c0 was parsed."],"exampleFix":"// before\ndata = await zhihu_client.get(uri, params=params)\n// after\nif \"d_c0\" not in zhihu_client.cookie_dict:\n    raise RuntimeError(\"zhihu cookie missing d_c0 - re-export cookie\")\ndata = await zhihu_client.get(uri, params=params)","handlingStrategy":"validation","validationCode":"def cookie_dict_has_d_c0(cookie_dict: dict) -> bool:\n    return bool(cookie_dict.get(\"d_c0\"))","typeGuard":"def has_d_c0(cookie_dict: dict) -> bool:\n    return isinstance(cookie_dict, dict) and isinstance(cookie_dict.get(\"d_c0\"), str) and len(cookie_dict[\"d_c0\"]) > 0","tryCatchPattern":"try:\n    data = await zhihu_client.get(uri, params=params)\nexcept Exception as e:\n    if \"d_c0\" in str(e):\n        raise RuntimeError(\"zhihu cookie lacks d_c0 - re-export full cookie from browser\") from e\n    raise","preventionTips":["Always export cookies after fully loading zhihu.com so d_c0 is set.","Validate the presence of d_c0 (names only, never values) right after login/cookie load.","Keep the cookie parser's key=value split consistent with how the string was exported."],"tags":["zhihu","cookies","signing","login"],"backgroundTag":null,"analyzedSha":"d6f7c5bb906b6dac40ddf343ef9e26438a3de092","analyzedAt":"2026-08-15T01:39:07.505Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}