Files
RSSHub/lib/v2/baidu/search.js
T
CaoMeiYouRen bbee9f247d feat(route): 新增 bing 搜索; 迁移 bing 每日图片到 v2; 新增 百度搜索; 迁移 搜狗特色LOGO 到 v2 规范;添加 搜狗搜索;添加 Google Search (#13936)
* fix(route): 修复 米游社 公告栏 template 错误

* feat(route): 新增 bing 搜索

* docs: Update other.mdx

* docs: fix docs

* feat(route): 新增 百度搜索

* fix(route): 修复 pubDate 解析错误

* fix(route): 优化 百度搜索的缓存,减轻反爬问题

* feat(route): 新增 360 搜索

* feat(route): 迁移 搜狗特色LOGO 到 v2 规范;添加 搜狗搜索

* fix(route): 百度搜索增加图片

* feat(route): 新增 Google Search

* fix(route): 修复 百度搜索相关问题

* fix(route): 修复 Google 相关问题

* fix(route): 修复 360 搜索

* fix(route): 修复 搜狗搜索

* fix(route): 修复 await 问题

* fix: 移除 google sites

* fix(route): 修复 缓存和过滤逻辑问题

* fix(route): 修复 360 搜索缺少 cookie 的问题

* fix(route): 修复 360 搜索 cookie 的问题

* feat(route): 移除 so.com 路由

* fix: merge conflict

---------
2023-12-25 23:14:21 +08:00

55 lines
2.2 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { art } = require('@/utils/render');
const path = require('path');
const renderDescription = (description, images) => art(path.join(__dirname, './templates/description.art'), { description, images });
const config = require('@/config').value;
module.exports = async (ctx) => {
const { keyword } = ctx.params;
const url = `https://www.baidu.com/s?wd=${encodeURIComponent(keyword)}`;
const key = `baidu-search:${url}`;
const items = await ctx.cache.tryGet(
key,
async () => {
const response = (await got(url)).data;
const visitedLinks = new Set();
const $ = cheerio.load(response);
const contentLeft = $('#content_left');
const containers = contentLeft.find('.c-container');
return containers
.map((i, el) => {
const element = $(el);
const link = element.find('h3 a').first().attr('href');
if (link && !visitedLinks.has(link)) {
visitedLinks.add(link);
const imgs = element
.find('img')
.map((_j, _el) => $(_el).attr('src'))
.toArray();
const description = element.find('.c-gap-top-small [class^="content-right_"]').first().text() || element.find('.c-row').first().text() || element.find('.cos-row').first().text();
return {
title: element.find('h3').first().text(),
description: renderDescription(description, imgs),
link: element.find('h3 a').first().attr('href'),
author: element.find('.c-row .c-color-gray').first().text() || '',
};
}
return null;
})
.toArray()
.filter((e) => e?.link);
},
config.cache.routeExpire,
false
);
ctx.state.data = {
title: `${keyword} - 百度搜索`,
description: `${keyword} - 百度搜索`,
link: url,
item: items,
};
};