diff --git a/docs/other.md b/docs/other.md index 1807134cab..72f35c707c 100644 --- a/docs/other.md +++ b/docs/other.md @@ -14,7 +14,7 @@ pageClass: routes ### 搜索文章 - + ## 99% Invisible diff --git a/lib/routes/36kr/search/article.js b/lib/routes/36kr/search/article.js index 410c14d433..409f26b7cc 100644 --- a/lib/routes/36kr/search/article.js +++ b/lib/routes/36kr/search/article.js @@ -2,9 +2,27 @@ const got = require('@/utils/got'); module.exports = async (ctx) => { const { keyword = '' } = ctx.params; + const csrfresponse = await got.get('https://36kr.com/pp/api/csrf'); + const cookies = csrfresponse.headers['set-cookie'].toString(); + const token = cookies.match(/M-XSRF-TOKEN=.{64}/)[0]; + const acw_tc = cookies.match(/acw_tc=.{61}/)[0]; + const krnewsfrontss = cookies.match(/krnewsfrontss=.{32}/)[0]; + const response = await got({ - method: 'get', - url: `https://36kr.com/pp/api/search/entity-search?page=1&per_page=20&sort=date&entity_type=post&keyword=${encodeURIComponent(keyword)}`, + method: 'post', + url: `https://36kr.com/pp/api/search/entity-search`, + headers: { + 'Content-Type': 'application/json', + cookie: `${token};${acw_tc};${krnewsfrontss};`, + 'M-X-XSRF-TOKEN': token.replace('M-XSRF-TOKEN=', ''), + }, + data: JSON.stringify({ + page: 1, + per_page: 20, + sort: 'date', + entity_type: 'post', + keyword: encodeURIComponent(keyword), + }), }); const load = async (link) => { @@ -12,8 +30,9 @@ module.exports = async (ctx) => { const description = response.data.data.content; return { description }; }; + const items = await Promise.all( - response.data.data.items.map(async (item) => { + (response.data.data.items || []).map(async (item) => { const link = `https://36kr.com/api/post/${item.id}`; const single = { title: item.title,