从js中用node.js和horseman刮取html

我正试图从这个页面的工资信息刮面糊数组: https : //www.swishanalytics.com/optimus/mlb/dfs-batter-projections

我正在使用node.js和node-horseman。

这是我的代码:

var Horseman = require('node-horseman'); var horseman = new Horseman(); horseman.open('https://www.swishanalytics.com/optimus/mlb/dfs-batter-projections'); if (horseman.status() === 200) { console.log('[+] Successful page opening') horseman.screenshot('image.png'); console.log(horseman.html()); } horseman.close(); 

问题是从horseman.html返回()仍然是很多的JavaScript,不能提取像cheerio的东西。 我怎样才能以编程方式执行JavaScript?

例如,如果我在相同的链接上查看源代码,则可以看到包含打击者的区域开始

 function Model(){ this.batterArray = [{"team_short":"rockies","mlbam_id":"571448","player_name":"Nolan Arenado", 

显然,这仍然是JavaScript …我假设在某些时候,它必须执行并转换为HTML浏览器呈现?

我只是testing了这一点,它似乎工作:

 var Horseman = require('node-horseman'); var horseman = new Horseman(); horseman.open('https://www.swishanalytics.com/optimus/mlb/dfs-batter-projections'); if (horseman.status() === 200) { console.log('[+] Successful page opening') horseman.screenshot('image.png'); var batters = horseman.evaluate(function(){ return (new Model()).batterArray; }); console.log(batters); } horseman.close(); 

这会给你一个你可以在你的代码中使用的打击者数组。 你可以写出来一个文件或创build一个表。

这是如何工作的。

 var Horseman = require('node-horseman'); var horseman = new Horseman(); horseman .open('https://www.swishanalytics.com/optimus/mlb/dfs-batter-projections') .status() .then((status) => { if(status === 200){ console.log('[+] Successful page opening') horseman.screenshot('image.png'); var batters = horseman.evaluate(function(){ return (new Model()).batterArray; }); console.log(batters); }else{ console.log('no batters'); } }) .close();