openclaw Windows版本部署、重装超详细全流程(附带基础操作命令)并将爬虫脚本(附带爬取微信公账号示例脚本)接入openclaw

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活


🧹 0) 清理旧安装

删除所有旧文件、旧服务、旧 PATH 设置,避免 npm 安装冲突

1) 终止任何旧的 OpenClaw 进程

taskkill /F /IM openclaw.exe 2>$null
taskkill /F /IM openclaw* 2>$null

2) 删除旧安装目录

假设你目前安装在这类目录:

  • F:\TEcode\ai-docx\.openclaw
  • F:\TEcode\ai-docx\openclaw
  • %USERPROFILE%\.openclaw
  • %APPDATA%\npm\node_modules\openclaw*

运行:

Remove-Item -Recurse -Force "F:\TEcode\ai-docx\.openclaw"
Remove-Item -Recurse -Force "F:\TEcode\ai-docx\openclaw"
Remove-Item -Recurse -Force "$env:USERPROFILE\.openclaw"

如果全局安装过 openclaw,还可以:

npm uninstall -g openclaw

3) 清理 npm 缓存(强制)

npm cache clean --force

4) 删除旧 PATH 设置

打开 PowerShell → 输入:

$env:PATH.Split(";") | Select-String "openclaw"
$env:PATH.Split(";") | Select-String "npm"

手工检查系统 用户 PATH/系统 PATH 里不要有旧的 openclaw 条目。


重新安装openclaw

1) 确保 Node.js 环境正确

OpenClaw 需要 Node.js 22 或更高版本。先检查:

node -v
npm -v

如果没有或版本过旧:

  1. 去官方下载安装最新的 Node.js LTS(当前至少 v22+)
    https://nodejs.org/
  2. 安装完成后重新打开 PowerShell 验证版本:
node -v
npm -v

2) 使用 npm 全局安装 OpenClaw CLI

全局安装命令:

npm install -g openclaw@latest

注意:包名是 openclaw(不是 @openclaw/cli)。


3) 验证安装是否成功

关闭所有 PowerShell 窗口,重新打开一个新的,再运行:

openclaw --version

如果没有输出版本号:

  • 可能全局 npm bin 路径不在 PATH 中
    运行:

    npm bin -g
    

    把输出的目录手动加到 用户环境变量 PATH 里。


4) 初始化 OpenClaw

安装成功后,第一次完整配置需要运行 onboarding 向导:

openclaw onboard

这个向导会让你:

  • 生成默认配置
  • 填写 API 密钥(Anthropic / OpenAI / Gemini 等)
  • 配置默认 agent 和 Gateway
  • OpenClaw 安装向导↓

OpenClaw 安装向导

  1. Personal use acknowledgment

    • 选项:
      ○ Yes / ● No
    • 答案:Yes
    • 作用:确认理解 OpenClaw 默认是个人使用,多人共享需额外安全设置。
  2. Onboarding mode

    • 选项:
      ● QuickStart / ○ Manual
    • 答案:QuickStart
    • 作用:快速完成初始化,后续可通过 openclaw configure 调整配置。
  3. Model/auth provider

    我这里选择了千文,会自动跳转到浏览器确认页面

  4. Default model after Qwen OAuth

    • 选项:
      ● Keep current (qwen-portal/coder-model) / ○ Enter model manually / ○ qwen-portal/coder-model / ○ qwen-portal/vision-model
    • 答案:Keep current (qwen-portal/coder-model)
    • 作用:保留默认的 qwen-portal/coder-model 模型,适合代码和文本处理。
  5. Select channel (QuickStart)

    • 选项:
      ● Telegram / ○ WhatsApp / ○ Discord / … / ○ Skip for now
    • 答案:Skip for now
    • 作用:暂不配置聊天平台集成,后续可通过 openclaw configure 添加。
  6. Search provider

    • 选项:
      ● Brave Search / ○ Gemini / ○ Grok / … / ○ Skip for now
    • 答案:Skip for now
    • 作用:暂不配置搜索提供商,保持基础功能。
  7. Configure skills now?

    • 选项:
      ● Yes / ○ No
    • 答案:No
    • 作用:跳过技能配置,后续可按需添加。
  8. Enable hooks?

    • 选项:
      ◻ Skip for now / ◻ boot-md / ◻ bootstrap-extra-files / ◻ command-logger / ◻ session-memory
    • 答案:Skip for now
    • 作用:暂不启用 hooks,保持安装干净。
  9. Gateway service already installed

    • 选项:
      ● Restart / ○ Reinstall / ○ Skip
    • 答案:Reinstall
    • 作用:重新安装 Gateway 服务,保证环境干净和配置正确。
  10. How do you want to hatch your bot?

    • 选项:
      ● Hatch in TUI / ○ Open the Web UI / ○ Do this later
    • 答案:Do this later
    • 作用:暂不创建或启动 bot,先完成基础安装。
  11. Start TUI / Hatch bot after onboarding complete

    • 选项:
      Do this later
    • 答案:Do this later
    • 作用:完成 onboarding 后暂不启动 TUI 或 bot,可随时通过 Web UI 管理 agent。

如不想立即运行向导,也可以先创建基础配置:

openclaw init

5) 启动 OpenClaw 服务

通常启动 Gateway 服务:

openclaw gateway --port 18789

你也可以启动 dashboard(自带管理 UI):

openclaw dashboard

爬取公众号接入

一、安装依赖

进入任意工具目录,例如:

F:\TEcode\tools\wechat-crawler

初始化:

npm init -y

安装依赖:

npm install playwright

安装浏览器:

npx playwright install

二、创建爬取脚本

创建文件:wechat.js

/**
 * wechat.js
 * 微信公众号文章爬取脚本(Playwright + Node.js)
 *
 * 功能:
 * - 获取文章标题、作者、正文内容
 * - 提取正文中的图片 URL
 * - 自动清理多余空行
 * - 返回 JSON
 * - 支持命令行参数 URL
 *
 * 使用:
 * node wechat.js <文章URL>
 *
 * 例如:
 * node wechat.js "https://mp.weixin.qq.com/s/xxxxx"
 */

const { chromium } = require("playwright");

/**
 * 清理文本:去除多余空行和前后空格
 * @param {string} text
 * @returns {string}
 */
function cleanText(text) {
  return text
    .split(/\r?\n/)
    .map(line => line.trim())
    .filter(line => line.length > 0)
    .join("\n");
}

/**
 * 爬取微信公众号文章
 * @param {string} url 公众号文章链接
 * @returns {Promise<object>} 返回结构化 JSON
 */
async function scrape(url) {
  // 启动浏览器
  const browser = await chromium.launch({
    headless: true,
    args: ["--disable-blink-features=AutomationControlled"] // 避免被检测
  });

  const page = await browser.newPage();

  // 访问公众号文章
  await page.goto(url, { waitUntil: "domcontentloaded" });

  // 提取标题、作者、正文、图片
  const data = await page.evaluate(() => {
    const title = document.querySelector("#activity-name")?.innerText || "";
    const author = document.querySelector("#js_name")?.innerText || "";
    const contentEl = document.querySelector("#js_content");
    const content = contentEl?.innerText || "";

    // 提取正文图片 URL
    const images = contentEl
      ? Array.from(contentEl.querySelectorAll("img")).map(img => img.src)
      : [];

    return { title, author, content, images };
  });

  await browser.close();

  // 清理正文文本
  data.content = cleanText(data.content);

  return data;
}

/**
 * 批量抓取(可选)
 * 输入 URL 数组,返回数组 JSON
 * @param {string[]} urls
 */
async function scrapeBatch(urls) {
  const results = [];
  for (const url of urls) {
    try {
      const res = await scrape(url);
      results.push({ url, ...res });
    } catch (err) {
      results.push({ url, error: err.message });
    }
  }
  return results;
}

/**
 * 主函数:命令行使用
 */
(async () => {
  const args = process.argv.slice(2);

  if (args.length === 0) {
    console.error("请提供微信公众号文章 URL,例如:");
    console.error('node wechat.js "https://mp.weixin.qq.com/s/xxxxx"');
    process.exit(1);
  }

  // 支持单个 URL 或多 URL
  const urls = args;

  let output;
  if (urls.length === 1) {
    try {
      output = await scrape(urls[0]);
    } catch (err) {
      console.error("抓取失败:", err.message);
      process.exit(1);
    }
  } else {
    output = await scrapeBatch(urls);
  }

  // 输出 JSON
  console.log(JSON.stringify(output, null, 2));
})();

三、本地运行测试

node wechat.js "https://mp.weixin.qq.com/s/xxxxx"

输出示例:
{
  "title": "某篇公众号文章标题",
  "author": "某公众号",
  "content": "文章正文内容..."
}

四、接入openclaw

OpenClaw 的自定义工具(skill/tool)可以包装你的脚本,让 agent 调用:

Agent
  ↓
调用工具 scrape_wechat
  ↓
执行 node wechat.js <URL>
  ↓
返回 JSON(title, author, content)
  ↓
LLM 总结或进一步处理

Agent 目录创建工具

假设你的 Agent 路径是:

F:\TEcode\ai-docx\openclaw\.openclaw\agents\main

tools 目录下创建一个文件:

F:\TEcode\ai-docx\openclaw\.openclaw\agents\main\tools\scrape_wechat.js

内容示例:

import { execSync } from "child_process";

export default {
  name: "scrape_wechat",
  description: "Fetch a WeChat public article by URL and return structured JSON",

  parameters: {
    type: "object",
    properties: {
      url: { type: "string", description: "微信公众号文章链接" }
    },
    required: ["url"]
  },

  run: async ({ url }) => {
    try {
      const result = execSync(`node F:\\TEcode\\tools\\wechat-crawler\\wechat.js "${url}"`).toString();
      return result;
    } catch (err) {
      return `抓取失败: ${err.message}`;
    }
  }
};

注意:路径替换为你本地 wechat.js 脚本实际存放位置。


重新加载 Agent&测试调用

对agent说:我写了一个爬取文章的脚本,放在了xxx下,请你把它配置为你的技能并重启

  • 确保 OpenClaw 检测到新的 scrape_wechat 工具。
  • 之后你可以在 TUI 或 Dashboard 里查看 agent 可用的工具列表。

在 Dashboard 对 agent 说:

抓取这篇文章: https://mp.weixin.qq.com/s/xxxxx

返回示例:

{
  "title": "文章标题",
  "author": "公众号名称",
  "content": "文章正文内容..."
}

可选:自动总结

如果希望 agent 直接返回摘要而不是原文,可以在工具里改写 run 函数:

return {
  summary: `请帮我总结这篇文章:\n${result}`
};

或者让 agent 调用 LLM:

agent.receive(scrape_wechat(url))
agent.summarize()

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值