Javascript 使用node.js和horseman从js中删除html
我正试图从本页中获取一系列的击球手薪资信息: 我正在使用node.js和node horseman 这是我的密码:Javascript 使用node.js和horseman从js中删除html,javascript,node.js,dom,phantomjs,Javascript,Node.js,Dom,Phantomjs,我正试图从本页中获取一系列的击球手薪资信息: 我正在使用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) {
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,然后由浏览器显示 我刚刚测试了一下,它似乎有效:
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();
这将为您提供一个可以在代码中使用的击球员数组。您可以将其写入文件或创建一个表。这就是它的工作原理
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();
钉死它。。。我在想盒子里的死点。当时它确实起作用了。swishanalytics.com的开发者可能已经更改了页面,这可能破坏了这个示例。这可以通过cheerio来完成,如果你感兴趣,我可以告诉你怎么做。它将更易于使用,并返回文本或json,但它不会像PhantomJS那样进行屏幕截图;很好用。