# 如何用RSSHub创建自定义RSS源?

RSSHub 是一个开源的、轻量的 RSS 生成器，它通过定义路由规则来抓取和生成各类网站的 RSS 源。而 RSSHub Radar 是一个浏览器扩展，它可以帮助你快速生成适合当前网页的 RSSHub 路由。

以下是使用 RSSHub Radar 的简要步骤：

### 安装 RSSHub Radar

1. **安装浏览器扩展**：
    
    * 进入浏览器的扩展商店，搜索并安装 RSSHub Radar 扩展。
        
    * [RSSHub Radar for Chrome](https://chrome.google.com/webstore/detail/rsshub-radar/kefjpfngnndepjbopdmoebkipbgkggaa)
        
    * [RSSHub Radar for Firefox](https://addons.mozilla.org/en-US/firefox/addon/rsshub-radar/)
        

### 使用 RSSHub Radar 生成 RSS 源

1. **访问你要监控的网页**：
    
    * 打开浏览器并访问你想要生成 RSS 源的网页。
        
2. **启动 RSSHub Radar**：
    
    * 在网页加载完成后，点击浏览器工具栏上的 RSSHub Radar 图标。它会自动检测该网页是否支持 RSSHub 路由规则。
        
3. **选择合适的路由规则**：
    
    * RSSHub Radar 会列出该网页支持的所有 RSSHub 路由规则。选择适合你的路由规则，点击生成对应的 RSS 源链接。
        
4. **订阅生成的 RSS 源**：
    
    * 复制生成的 RSS 源链接，并在你喜欢的 RSS 阅读器中订阅。
        

### 示例

假设你想监控某个新闻网站的特定栏目，可以按照以下步骤进行：

1. **打开目标网站的栏目页面**，例如 [`https://example.com/news/technology`。](https://example.com/news/technology%E3%80%82)
    
2. **点击 RSSHub Radar 图标**，查看可用的路由规则。
    
3. **选择适合的路由规则**，例如 `/example/news/:category`。
    
4. **复制生成的 RSS 源链接**，例如 [`https://rsshub.app/example/news/technology`。](https://rsshub.app/example/news/technology%E3%80%82)
    
5. **在 RSS 阅读器中订阅该链接**。
    

### 自定义 RSSHub 规则

如果你发现 RSSHub 没有预定义的规则来监控你需要的网站，你可以考虑自己添加新的路由规则。以下是一个基本的步骤：

1. **Fork RSSHub 仓库**：
    
    * 访问 [RSSHub GitHub 仓库](https://github.com/DIYgod/RSSHub) 并 fork 一份到你的账号下。
        
2. **添加新路由规则**：
    
    * 根据 [RSSHub 文档](https://docs.rsshub.app/en/) 添加新的网站路由规则。
        
    * 路由规则通常位于 `lib/routes/` 目录下。
        
3. **测试你的规则**：
    
    * 本地运行 RSSHub，确保你添加的规则工作正常。
        
4. **提交 Pull Request**：
    
    * 将你的改动提交回原始的 RSSHub 仓库，等待合并。
        

通过这些步骤，你可以轻松地利用 RSSHub 和 RSSHub Radar 来监控网页，并生成 RSS 源，比手动编写脚本要简单得多。

下面是一个详细的示例，展示如何为 RSSHub 添加自定义规则，以生成你需要的 RSS 源。

### 示例网站

假设我们要为一个虚构的新闻网站 [`example.com`](http://example.com) 开发自定义 RSSHub 规则，该网站有以下页面结构：

* [`https://example.com/news`](https://example.com/news) - 新闻主页，包含最新新闻列表
    
* [`https://example.com/news/:category`](https://example.com/news/:category) - 特定分类的新闻列表，例如：[`https://example.com/news/technology`](https://example.com/news/technology)
    

### 开始开发

1. **Fork 和 Clone RSSHub 仓库**
    
    首先，fork [RSSHub 仓库](https://github.com/DIYgod/RSSHub) 并将其克隆到本地：
    
    ```bash
    git clone https://github.com/<your-username>/RSSHub.git
    cd RSSHub
    ```
    
2. **安装依赖**
    
    安装项目依赖：
    
    ```bash
    npm install
    ```
    
3. **创建新路由文件**
    
    在 `lib/routes/` 目录下为你的目标网站创建一个新的目录，例如 `example`，并在其中创建一个 `news.js` 文件：
    
    ```bash
    mkdir lib/routes/example
    touch lib/routes/example/news.js
    ```
    
4. **编写路由规则**
    
    编辑 `news.js` 文件，添加以下内容：
    
    ```javascript
    const got = require('@/utils/got');
    const cheerio = require('cheerio');
    const { parseDate } = require('@/utils/parse-date');
    
    module.exports = async (ctx) => {
        const category = ctx.params.category || 'latest';
        const url = `https://example.com/news/${category}`;
        const response = await got(url);
        const $ = cheerio.load(response.data);
    
        const list = $('.news-item').map((_, item) => {
            item = $(item);
            const title = item.find('.news-title').text();
            const link = item.find('.news-title a').attr('href');
            const pubDate = parseDate(item.find('.news-date').text());
    
            return {
                title,
                link,
                pubDate,
            };
        }).get();
    
        ctx.state.data = {
            title: `Example News - ${category}`,
            link: url,
            item: list,
        };
    };
    ```
    
    这里我们使用 `got` 来获取网页内容，使用 `cheerio` 来解析 HTML，并提取新闻标题、链接和发布日期。
    
5. **注册路由**
    
    编辑 `lib/router.js` 文件，添加你的新路由：
    
    ```javascript
    module.exports = (router) => {
        router.get('/example/news/:category?', require('./routes/example/news'));
    };
    ```
    
6. **本地运行 RSSHub**
    
    启动 RSSHub 以测试你的新路由：
    
    ```bash
    npm start
    ```
    
    在浏览器中访问 [`http://localhost:1200/example/news/technology`，应该可以看到生成的](http://localhost:1200/example/news/technology%EF%BC%8C%E5%BA%94%E8%AF%A5%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E7%94%9F%E6%88%90%E7%9A%84) RSS 源。
    
7. **提交代码并创建 Pull Request**
    
    如果你的自定义规则工作正常，可以将更改提交并推送到你的 fork 仓库：
    
    ```bash
    git add .
    git commit -m "Add custom RSS rule for example.com news"
    git push origin master
    ```
    
    然后到 GitHub 上为原始的 RSSHub 仓库创建一个 Pull Request。
    

### 完整代码示例

**lib/routes/example/news.js**

```javascript
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
    const category = ctx.params.category || 'latest';
    const url = `https://example.com/news/${category}`;
    const response = await got(url);
    const $ = cheerio.load(response.data);

    const list = $('.news-item').map((_, item) => {
        item = $(item);
        const title = item.find('.news-title').text();
        const link = item.find('.news-title a').attr('href');
        const pubDate = parseDate(item.find('.news-date').text());

        return {
            title,
            link,
            pubDate,
        };
    }).get();

    ctx.state.data = {
        title: `Example News - ${category}`,
        link: url,
        item: list,
    };
};
```

**lib/router.js**

```javascript
module.exports = (router) => {
    router.get('/example/news/:category?', require('./routes/example/news'));
};
```

通过这个示例，你可以学习到如何为一个特定的网站添加自定义的 RSS 规则。你可以根据不同网站的结构和需求调整代码，以满足你具体的需求。
