问题与NPM需要
我运行一个Nightmare.js脚本,并在其中我试图require
lodash(本地安装npm,在node_modules中,在package.json中注册)。
index.js:
var Nightmare = require('nightmare'); var _ = require('lodash'); function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } function getElms(elm) { var elsm = document.querySelectorAll(elm); return _.map(elsm, function(elem){ return elem.innerHTML; }); } var someWebsite = new Nightmare({ show: false }) .goto( ***some url*** ) .wait(getRandomInt(2500, 5000)) .evaluate(function () { var elsm = document.querySelectorAll(***some selector***); return _.map(elsm, function(elem){ return elem.innerHTML; }); // return getElms(***some selector***); }).then(function(linkstexts) { console.log(linkstexts) });
-
但我得到这个:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):_未定义
- 类似的问题,如果我取消注册并使用
return getElms(***some selector***);
- 类似的问题,如果我取消注册并使用
这是package.json
{ "name": "nightxp", "version": "1.0.0", "description": "", "main": "index.js", "dependencies": { "lodash": "^4.17.4", "nightmare": "^2.10.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
我错过了什么?
UPDATE
我尝试将lodash传递给浏览器作用域,并用catch()
完成,但不知何故lodashvariables仍然不被传递给作用域(例如hackernews):
var Nightmare = require('nightmare'); var lodash = require('lodash'); function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } var facebook = new Nightmare({ show: false }) .goto('https://news.ycombinator.com') .wait(getRandomInt(5500, 6000)) .evaluate( (lodash) => { var elsm = document.querySelectorAll('tr td.title a.storylink'); return lodash.map(elsm, function(elem){ return elem.innerHTML; }); }, lodash).then(function(titles) { console.log(titles) }).catch(function (error) { console.error('Error:', error); });
这是我得到的:
C:\Projects\nightmare>set DEBUG=nightmare & node index.js nightmare queuing process start +0ms nightmare queueing action "goto" for https://news.ycombinator.com +4ms nightmare queueing action "wait" +2ms nightmare queueing action "evaluate" +0ms nightmare running +1ms Error: Cannot read property 'map' of undefined
当你跑步
var someWebsite = new Nightmare({ show: false }) .evaluate(function () { /* ... */ }
您现在处于浏览器范围内 – 您可以将Nightmare.evaluate
视为在独立浏览器的上下文中运行包含的代码,这意味着您的全局范围(包括_
)不可用。
在这里看到更多的信息和一些关于将数据传递到范围的提示:
https://github.com/segmentio/nightmare/issues/89
https://github.com/segmentio/nightmare/issues/333
编辑:
虽然可以将简单值传递到节点上下文中,但不能将复杂值与函数和方法一起传递。 这是因为参数在传输中是序列化的,而这对于函数来说是很困难的。 这里有一个问题可以解释一下:
https://github.com/segmentio/nightmare/issues/349
在这个特殊的情况下,似乎最简单的做法就是在没有问题的情况下工作,这些日子并不困难。 例如:
var facebook = new Nightmare({ show: false }) .goto('https://news.ycombinator.com') .wait(getRandomInt(5500, 6000)) .evaluate( () => { var elsm = document.querySelectorAll('tr td.title a.storylink'); return Array.prototype.map.call(elsm, elem => elem.innerHTML) }) .then(function(titles) { console.log("title", titles) }) .catch(function (error) { console.error('Error:', error); });