Nodejs google二级域名站内搜索

Nodejs google二级域名站内搜索

google貌似不支持二级域名站内搜索,我的网站部署在BAE上的,有什么解决办法吗?

3 回复

当然可以。Google确实没有直接支持二级域名的站内搜索功能。但是,你可以通过使用Google Custom Search Engine (CSE) 来实现这一需求。Google CSE 允许你创建一个自定义的搜索引擎,可以指定特定的站点或子域进行搜索。

以下是一个使用Node.js和Google Custom Search API 实现二级域名站内搜索的简单示例。

步骤 1: 创建 Google Custom Search Engine

  1. 登录到 Google Custom Search 控制台(https://cse.google.com/cse/)。
  2. 点击“新建搜索引擎”并填写必要的信息。
  3. 在“搜索范围”中选择“指定网站”,然后输入你的二级域名,例如 *.yourdomain.com
  4. 完成设置后,你会得到一个搜索引擎ID(例如:012345678901234567890:abcdefg)。

步骤 2: 使用 Node.js 获取搜索结果

接下来,你需要在Node.js项目中安装Google Custom Search API客户端库。你可以使用npm来安装:

npm install googleapis --save

然后,在你的Node.js应用中使用以下代码来获取搜索结果:

const {google} = require('googleapis');

async function searchCustomEngine(query, searchEngineId) {
    const customsearch = google.customsearch({version: 'v1', auth: 'YOUR_API_KEY'});
    
    try {
        const res = await customsearch.cse.list({
            q: query,
            cx: searchEngineId,
        });
        
        console.log(res.data.items); // 这里是搜索结果
    } catch (error) {
        console.error(error);
    }
}

// 使用示例
const searchEngineId = '012345678901234567890:abcdefg';
const query = 'example search query';

searchCustomEngine(query, searchEngineId);

解释

  • Google Custom Search API:允许你查询Google自定义搜索引擎。
  • cx: 这是你之前创建的自定义搜索引擎ID。
  • auth: 你需要一个有效的API密钥来访问Google Custom Search API。你可以在Google Cloud Console中生成这个密钥。

这样,你就可以在你的Node.js应用中实现对特定二级域名的站内搜索了。


用 CNAME 转一下。

要在Node.js中实现对包含二级域名的网站进行站内搜索,并且处理Google可能不支持二级域名搜索的情况,可以考虑使用自定义的方法或服务。以下是一种可行的方法:

解决方案

我们可以使用Node.js来爬取网站的所有页面,并将这些页面的内容存储在一个数据库中。然后创建一个简单的搜索功能,允许用户通过关键词搜索这些页面。

步骤概述

  1. 爬虫:使用Node.js爬虫库(如puppeteercheerio)来抓取所有页面。
  2. 存储:将抓取到的页面内容存储到数据库中(如MongoDB)。
  3. 搜索功能:创建一个简单的搜索API,用户可以通过这个API查询数据库中的内容。

示例代码

以下是使用axioscheerio抓取页面内容并存储到MongoDB的基本示例:

const axios = require('axios');
const cheerio = require('cheerio');
const mongoose = require('mongoose');

// 连接到MongoDB
mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

// 定义页面模型
const PageSchema = new mongoose.Schema({
    url: String,
    content: String
});

const Page = mongoose.model('Page', PageSchema);

async function crawlAndStore() {
    const startUrl = 'http://example.com'; // 替换为你的二级域名起始URL
    const response = await axios.get(startUrl);
    const $ = cheerio.load(response.data);

    // 抓取页面内容并保存
    $('a').each((index, element) => {
        const href = $(element).attr('href');
        if (href && href.startsWith('/')) {
            const fullUrl = `${startUrl}${href}`;
            Page.findOne({ url: fullUrl }).then(page => {
                if (!page) {
                    axios.get(fullUrl).then(res => {
                        const pageContent = res.data;
                        const newPage = new Page({ url: fullUrl, content: pageContent });
                        newPage.save();
                    });
                }
            });
        }
    });
}

crawlAndStore().catch(console.error);

搜索功能

接下来,你需要创建一个简单的搜索API,该API接收一个关键词作为参数,并返回包含该关键词的页面。

app.get('/search', async (req, res) => {
    const keyword = req.query.keyword;
    const results = await Page.find({ content: { $regex: new RegExp(keyword, 'i') } });
    res.json(results);
});

以上代码仅作示例,实际应用中需要进一步优化错误处理、性能调整以及安全性改进。

回到顶部