无法在噩梦回调函数中使用节点模块
我试图在噩梦评估回调函数中使用节点模块,但每次运行代码时都会收到错误,提示 html2json is not Defined
。我尝试使用 import 和 require 来导入我的模块,但它不起作用。我还尝试使用闭包来传递模块并返回我使用该特定模块的回调函数,但它也不起作用。如果我在回调函数之外使用这个模块,它工作得很好。非常感谢对此的任何帮助。
import Nightmare from "nightmare";
import html2json from "html2json";
const nightmare = Nightmare({ show: false });
async function startScrap() {
let result = await nightmare
.goto("https://duckduckgo.com")
.evaluate(() => {
const html = html2json.html2json(
document.querySelector("#r1-0 a.result__a").innerHTML,
);
console.log(html);
return document.querySelector("#r1-0 a.result__a").href;
})
.end()
.then(function (result) {
console.log(result);
});
}
startScrap();
我每次都会遇到的错误:
node:internal/process/promises:265
triggerUncaughtException(err, true /* fromPromise */); rogramming\Personal Projects\scrappers\animelist-scrapper\test.js"
^
ReferenceError: html2json is not defined
at fn (<anonymous>:6:26)
at javascript (<anonymous>:25:21)
at <anonymous>:40:3
at EventEmitter.electron.ipcRenderer.on (d:\Programming\Personal Projects\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30) s\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30)
at emitMany (events.js:147:13)
at EventEmitter.emit (events.js:224:7) {
code: -1
}
I'm trying to use a node module in nightmare evaluate callback function but every time I run my code I get error which says html2json is not defined
. I tried using both import and require to import my module but It didn't work. I also tried using closure to pass the module and return the callback function where I have use that specific module but it didn't work either. If I use this module outside of callback function it works perfectly fine. Any help on this is greatly appreciated.
import Nightmare from "nightmare";
import html2json from "html2json";
const nightmare = Nightmare({ show: false });
async function startScrap() {
let result = await nightmare
.goto("https://duckduckgo.com")
.evaluate(() => {
const html = html2json.html2json(
document.querySelector("#r1-0 a.result__a").innerHTML,
);
console.log(html);
return document.querySelector("#r1-0 a.result__a").href;
})
.end()
.then(function (result) {
console.log(result);
});
}
startScrap();
The error that I get everytime :
node:internal/process/promises:265
triggerUncaughtException(err, true /* fromPromise */); rogramming\Personal Projects\scrappers\animelist-scrapper\test.js"
^
ReferenceError: html2json is not defined
at fn (<anonymous>:6:26)
at javascript (<anonymous>:25:21)
at <anonymous>:40:3
at EventEmitter.electron.ipcRenderer.on (d:\Programming\Personal Projects\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30) s\scrappers\animelist-scrapper\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:36:30)
at emitMany (events.js:147:13)
at EventEmitter.emit (events.js:224:7) {
code: -1
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在评估()中传递的函数在浏览器中运行。就像通过在网络浏览器中打开网页来运行该功能一样。因此,它无权访问节点模块。
方法是选择并提取您感兴趣的元素的属性,然后在 then() 中对其进行处理。
then() 将有权访问节点模块。
The function you pass in evaluate() is run in browser. Like, running the function by opening web page in your web browser. So, it does not have access to node modules.
Way to do it, is to select and extract the attributes of the elements you are interested in and then process it in then().
then() will have access to node modules.