NodeJs + PhantomJs从page.evaluate方法返回JQuery结果

NodeJS新手在这里。 我试图parsing使用NodeJS和PhamtomJS( phantomjs节点 )的HTML。 当我从浏览器控制台运行JQuery $("[class*='question-summary']") ,它返回一个数组。 但是,我无法在nodejs上做同样的事情。 我猜stackoverflow有JQuery所以我不需要使用includeJs来加载jQuery。 其实,当我跑步

这里是我正在运行的nodejs示例;

 var phantom = require('phantom'); async function getHtml() { const instance = await phantom.create([ "--load-images=false" ]); const page = await instance.createPage(); await page.on("onResourceRequested", function(requestData) { console.info('Requesting', requestData.url) }); const status = await page.open('http://stackoverflow.com'); console.log("STATUS: " + status); const content = await page.property('content'); console.log(content); var result = await page.evaluate(function(content) { return $("[class*='question-summary']"); }); console.log("Result : " + result); await instance.exit(); }; getHtml(); 

我运行命令>node --harmony-async-await phantomTest.js 。 打印内容到控制台后,进程卡住了。

在这里回答我自己的问题。 在评估函数内部创build一个数组,并在内部推送元素。 我想唯一的限制是phantom-node只是支持返回对象与基元。

 var result = await page.evaluate(function() { var questionSummaries = []; $("[class*='question-summary']").each(function() { questionSummaries.push(this.innerHTML); }); return questionSummaries; });