javascript - 使用 PhantomJS 找不到模块网页
问题描述
我正在使用 PhantomJS 在网页上搜索单词,我尝试将其设置为:
const phantomjs = require("phantomjs-prebuilt");
if (cmd === `${prefix}check`) {
let word = (args[0]);
var page = require('webpage').create();
page.open('https://discordapp.com/channels/000/000', function(err, data) {
if (err) throw err;
if (data.indexOf(word) >= 0) {
message.reply(word+ ' Found!');
} else {
message.reply(word+ ' Not found.');
}
});
}
但是我收到以下错误:
(节点:3520)UnhandledPromiseRejectionWarning:错误:找不到模块“网页”
这是什么原因造成的?
编辑
我刚刚看到它不适用于 Node JS,是否可以调用单独的 JS 文件并传递(args[0]);
?
解决方案
如果你想从 node.js 使用 PhantomJS,你可以,有几个包,其中之一是phantom。它支持 Promises 和 async/await 函数:
const phantom = require('phantom');
(async function() {
const instance = await phantom.create();
const page = await instance.createPage();
await page.on('onResourceRequested', function(requestData) {
console.info('Requesting', requestData.url);
});
const status = await page.open('https://stackoverflow.com/');
const content = await page.property('content');
console.log(content);
await instance.exit();
})();
您当然可以从命令行启动 PhantomJS 并向其传递必要的参数:
phantomjs script.js https://stackoverflow.com
var system = require('system');
var args = system.args;
if (args.length === 1) {
console.log('Try to pass some arguments when invoking this script!');
} else {
args.forEach(function(arg, i) {
console.log(i + ': ' + arg);
});
}
请注意,您使用page.open
错误,回调函数签名中没有data
var。如果要获取页面的所有内容,请参考page.content变量:
page.open('http://phantomjs.org', function (status) {
var content = page.content;
console.log('Content: ' + content);
phantom.exit();
});
推荐阅读
- python - 如何使用 diff 选择相近但在未知范围内的时间?
- r - R如何按组滞后数据框
- python-3.x - 使用 Beautifulsoup html.parser 解析页面返回空响应
- java - 与作为函数参数的端口号 8080 相关的“java.io.FileNotFoundException”错误
- java - 线程间共享的主体
- sgx - 如何安装英特尔 SGX 平台软件?
- go - 函数命中返回语句时不退出
- vue.js - 在 webpack 捆绑期间从源文件中提取字符串
- python-3.x - 我的装饰器改变了我的函数输出,如何避免这种情况?Python
- c# - Asp.Net Core 类不传递数据