Nodejs 安装crawler错误 求解
Nodejs 安装crawler错误 求解
E:\www\node-crawler\node_modules\crawler\node_modules\jsdom\node_modules\contextify>node “D:\Program Files\nodejs\node_modules\npm\bin\node-gyp-
bin\…\node_modules\node-gyp\bin\node-gyp.js” rebuild
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
E:\www\node-crawler\node_modules\crawler\node_modules\jsdom\node_modules\contextify\build\contextify.vcxproj(47,46): error MSB4025: 未能加载项目
文件。给定
编码中的字符无效。 第 47 行,位置 46。
gyp ERR! build error
gyp ERR! stack Error: C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (D:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:267:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Windows_NT 6.2.9200
gyp ERR! command “node” “D:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js” “rebuild”
gyp ERR! cwd E:\www\node-crawler\node_modules\crawler\node_modules\jsdom\node_modules\contextify
gyp ERR! node -v v0.10.24
gyp ERR! node-gyp -v v0.12.2
gyp ERR! not ok
针对你在安装 crawler
模块时遇到的错误,问题可能出在 contextify
的编译过程中,特别是在 Windows 系统上。contextify
是一个 Node.js 模块,用于在 V8 引擎中创建上下文环境,通常用于与 jsdom
结合使用来模拟浏览器环境。由于 contextify
需要通过 node-gyp
进行编译,因此可能会遇到一些依赖于 Visual Studio 的构建工具的兼容性问题。
解决方法
1. 安装必要的构建工具
确保你已经安装了 Visual Studio 的构建工具。可以通过运行以下命令安装:
npm install --global windows-build-tools
或者手动安装 Visual Studio 构建工具。如果你已经安装了 Visual Studio,确保安装了“C++”工作负载。
2. 更新 Node.js 和 npm
有时,使用较旧版本的 Node.js 或 npm 可能会导致编译问题。尝试更新到最新版本:
npm install -g npm
然后重新安装 Node.js 到最新版本。
3. 清理并重新安装模块
有时候,之前的安装可能留下了一些不完整或损坏的文件。你可以尝试清理并重新安装 crawler
模块:
cd E:\www\node-crawler
rm -rf node_modules
npm cache clean --force
npm install
4. 使用预编译二进制文件
如果上述方法仍然无法解决问题,可以尝试使用 contextify
的预编译二进制文件。你可以尝试使用 contextify-prebuilt
模块替换 contextify
:
npm uninstall contextify
npm install contextify-prebuilt
示例代码
假设你已经解决了安装问题,并且成功安装了 crawler
模块,你可以使用以下代码来测试 crawler
模块的功能:
const Crawler = require('crawler');
const c = new Crawler({
maxConnections : 10,
callback : function (error, res, done) {
if(error){
console.log(error);
}else{
const $ = res.$;
// 在这里处理解析后的数据
console.log($('title').text());
}
done();
}
});
// Queue just one URL, with default callback
c.queue('http://www.example.com');
这段代码会抓取 http://www.example.com
并打印页面的标题。如果一切正常,你应该能看到页面的标题输出到控制台。
希望这些步骤能够帮助你解决安装 crawler
模块时遇到的问题。
根据你提供的信息,错误发生在安装 crawler
模块时遇到的 contextify
编译问题。这通常是由于 Node-gyp 需要编译原生模块,但环境配置不正确导致的。以下是一些可能的解决方案:
解决方案
-
安装 Visual Studio Build Tools: 确保你已经安装了 Visual Studio 或者 Visual Studio Build Tools。
contextify
模块需要这些工具来编译。你可以通过命令行安装 Visual Studio Build Tools:
npm install --global windows-build-tools
-
检查 Node.js 和 npm 版本: 你当前使用的是较旧的 Node.js 版本(v0.10.24),建议更新到最新的 LTS 版本。
更新 Node.js:
nvm install --lts nvm use --lts
-
设置正确的环境变量: 确保
msbuild.exe
路径已添加到系统环境变量中。在命令提示符中运行:
set PATH=%PATH%;C:\Program Files (x86)\MSBuild\12.0\Bin
示例代码
如果上述方法都不能解决问题,可以尝试手动克隆 contextify
仓库并手动编译它:
git clone https://github.com/tmpvar/contextify.git
cd contextify
npm install
node-gyp configure
node-gyp build
然后尝试重新安装 crawler
模块:
npm install crawler
希望这些步骤能帮助你解决安装问题。如果问题仍然存在,请提供更多详细信息以便进一步排查。