首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用PhantomJS找到模块网页

无法使用PhantomJS找到模块网页
EN

Stack Overflow用户
提问于 2018-05-03 10:02:30
回答 1查看 767关注 0票数 1

我正在使用PhantomJS搜索网页上的单词,我试图设置如下:

代码语言:javascript
复制
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文件并传递(args[0]);

EN

回答 1

Stack Overflow用户

发布于 2018-05-03 11:03:45

如果您想使用来自PhantomJS的node.js,您可以,有几个软件包,其中之一是幻影。它支持许诺和异步/等待功能:

代码语言:javascript
复制
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并向其传递必要的参数:

代码语言:javascript
复制
phantomjs script.js https://stackoverflow.com

然后用system.args在脚本中接收它们

代码语言:javascript
复制
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变量。如果您想获取页面的所有内容,请参考page.content变量:

代码语言:javascript
复制
page.open('http://phantomjs.org', function (status) {
  var content = page.content;
  console.log('Content: ' + content);
  phantom.exit();
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50152263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档