diff --git a/lib/routes/zhihu/utils.ts b/lib/routes/zhihu/utils.ts index d5705c83c..b72468b76 100644 --- a/lib/routes/zhihu/utils.ts +++ b/lib/routes/zhihu/utils.ts @@ -62,9 +62,25 @@ export const getSignedHeader = async (url: string, apiPath: string) => { // fisrt: get cookie(dc_0) from zhihu.com const dc0 = await cache.tryGet('zhihu:cookies:d_c0', async () => { - const response = await ofetch.raw(url); + const response1 = await ofetch.raw(url); + const $ = load(response1._data); + const zseCk = $('script:contains("__zse_ck")') + .text() + .match(/\|\|"(.*?)",.*;document\.cookie/)?.[1]; + if (!zseCk) { + throw new Error('Failed to extract `__zse_ck` from page'); + } - const dc0 = response.headers + const response2 = await ofetch.raw(url, { + headers: { + cookie: `${response1.headers + .getSetCookie() + .map((s) => s.split(';')[0]) + .join('; ')}; __zse_ck=${zseCk}`, + }, + }); + + const dc0 = response2.headers .getSetCookie() .find((s) => s.startsWith('d_c0=')) ?.split(';')[0];