使用NodeJS从外部页面返回Javascript可变数据

我试图发送一个请求到一个页面,并抓住整个DOM。 基本上爬行。 在这个网站上,有一个variables直接加载到HTML(不是一个脚本文件)与一些数据。 使用我使用request NodeJS后端,我将如何请求这个页面并返回variables的数据? 这是一个例子:

http://some-page.com/index.html

 <html> <head> <script> var my_var = { title: "Good title", description: "Nice description", page: 5 }; </script> </head> </html> 

如果我访问网站,打开控制台并键入my_var我可以看到控制台中的内容,所以这是一个全局variables。

我怎么能做这样的事情? 如果需要,我可以使用另一个请求库。

您正在寻找jsdom: https : //github.com/tmpvar/jsdom

 const dom = new JSDOM(`<body> <script>document.body.appendChild(document.createElement("hr"));</script> </body>`, { runScripts: "dangerously" }); // The script will be executed and modify the DOM: dom.window.document.body.children.length === 2; 

它也带有一个虚拟控制台

虚拟控制台

像网页浏览器一样,jsdom具有“控制台”的概念。 这会logging从页面直接发送的信息,通过在文档中执行的脚本以及jsdom实现本身的信息。