学习Nodejs alsotang《使用 superagent 与 cheerio 完成简单爬虫》出错
学习Nodejs alsotang《使用 superagent 与 cheerio 完成简单爬虫》出错
学习 alsotang的《使用 superagent 与 cheerio 完成简单爬虫》过程中,安装superagent 和cheerio 都会有
npm ERR! cb() never cal
npm ERR! not ok code 0
这两个错误。
最后启动app.js时也出错:
module.js:340
throw err;
^
Error: Cannot find module 'inherits'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (E:\NODE-PROJECT\crawler\node_modules\superagent\node_
modules\readable-stream\lib\_stream_duplex.js:40:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
从你提供的信息来看,问题可能出在 superagent
和 cheerio
的安装过程中。错误提示表明在安装过程中遇到了 npm ERR! cb() never called
这样的错误,这通常意味着 npm 在处理某些操作时卡住了或者遇到了一些网络问题。同时,启动 app.js
时出现了 Error: Cannot find module 'inherits'
错误,这表明某些依赖项没有正确安装。
以下是解决这些问题的步骤:
1. 清理并重新安装依赖
首先,尝试清理 npm 缓存,并删除 node_modules
文件夹后重新安装依赖:
# 清理 npm 缓存
npm cache clean --force
# 删除 node_modules 文件夹
rm -rf node_modules
# 重新安装依赖
npm install
2. 确保 Node.js 版本兼容
确保你使用的 Node.js 版本与项目所需版本一致。有时候,特定版本的 Node.js 可能会导致某些包无法正常工作。你可以通过以下命令查看当前 Node.js 版本:
node -v
如果需要切换 Node.js 版本,可以考虑使用 nvm
(Node Version Manager)来管理不同版本的 Node.js。
3. 检查 package.json
确保你的 package.json
文件中包含 superagent
和 cheerio
的依赖项,并且版本号正确。例如:
{
"name": "your-project",
"version": "1.0.0",
"dependencies": {
"superagent": "^5.0.0",
"cheerio": "^1.0.0-rc.3"
}
}
然后再次运行 npm install
来安装这些依赖项。
示例代码
假设你已经解决了上述问题,并且成功安装了所有依赖项,这里是一个简单的示例代码,展示如何使用 superagent
和 cheerio
来创建一个简单的网页爬虫:
const superagent = require('superagent');
const cheerio = require('cheerio');
// 发送 HTTP GET 请求获取网页内容
superagent.get('https://example.com')
.then(response => {
const $ = cheerio.load(response.text);
// 使用 cheerio 解析 HTML
$('a').each((index, element) => {
console.log($(element).attr('href'));
});
})
.catch(error => {
console.error('请求失败:', error);
});
这个示例代码会向 https://example.com
发送一个 GET 请求,然后使用 cheerio
解析返回的 HTML,打印出页面中的所有链接。
希望这些步骤能帮助你解决问题!如果问题仍然存在,请提供更多的错误日志或详细信息以便进一步诊断。
Cannot find module 'inherits'
这样吧,你把 node_modules 文件夹删了。
然后npm install nrm -g, 安装nrm, 输入 nrm -h。 nrm 这个可以用来切换下载源,你再试试重新安装依赖模块。
Windows 7
下安装很顺利
我也在做这个lesson的时候出了问题,如图
在lesson3的文件夹中我分别都install了express, superagent和cheerio
代码如上,node新手刚入门
注意装依赖
从你的描述来看,你在安装 superagent
和 cheerio
时遇到了一些问题,并且在启动 app.js
时也出现了错误。具体错误是 Cannot find module 'inherits'
。这通常意味着某些依赖项没有正确安装。
首先确保你的 Node.js 和 npm 是最新版本。你可以通过以下命令来更新:
# 更新 npm
npm install -g npm
# 更新 Node.js(如果你使用的是 nvm)
nvm install --lts
nvm use --lts
接下来,尝试删除 node_modules
文件夹并重新安装所有依赖项:
# 删除 node_modules 文件夹
rm -rf node_modules
# 重新安装依赖
npm install
如果上述步骤仍然无法解决问题,请检查你的 package.json
文件是否包含所有必要的依赖项。确保 superagent
和 cheerio
的版本是兼容的。
以下是一个简单的示例,演示如何使用 superagent
和 cheerio
来抓取网页内容:
// app.js
const superagent = require('superagent');
const cheerio = require('cheerio');
async function fetchWebpage(url) {
try {
const response = await superagent.get(url);
const $ = cheerio.load(response.text);
// 抓取标题
const title = $('title').text();
console.log('Title:', title);
} catch (error) {
console.error('Error fetching webpage:', error.message);
}
}
fetchWebpage('https://example.com');
确保你已经安装了所需的依赖项:
npm install superagent cheerio
希望这些信息能帮助你解决问题!如果问题仍然存在,请提供更多的错误日志以便进一步诊断。