使用phantomjs调用函数会产生与从控制台调用不同的结果

时间:2016-11-10 00:05:02

标签: javascript phantomjs

我试图从这个page获得NBA球员统计数据。有一个UI按钮,允许您将数据表转换为csv,我试图自动化此过程。在幕后,它调用函数get_csv_output()

在检查器控制台中,get_csv_output("per_game")get_csv_output("advanced")分别以csv格式输出#per_game#advanced表。

但是,当我尝试使用phantom.js调用get_csv_output()函数时,它只会获取" per_game"的csv数据。表,但不适用于"高级"表

var page = require('webpage').create();
page.open('http://www.basketball-reference.com/players/a/abdulka01.html', function() {
    var result = page.evaluate(function() {
     return get_csv_output("per_game");
    });
    console.log(result);
    phantom.exit()
});

这是csv格式的per_game表,如预期的那样。但是,当我尝试将其更改为get_csv_output("advanced")时,

输出为Converting from PRE-Formatted to CSV does not work, please <span class=tooltip onClick="window.location.reload()">Reload</span> and then click CSV

我尝试提供一些其他表id作为输入,per_game似乎是唯一有效的。

1 个答案:

答案 0 :(得分:0)

问题解决了,现在可行了:

function on_init (page){
page.viewportSize = {width:1600,height:900}
page.evaluate(function (){
window.screen = {width:1600,height:900,availWidth:1600,availHeight:900};
window.innerWidth=1600;  window.innerHeight=900;   window.outerWidth=1600;  window.outerHeight=900;
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}},
mimeTypes: {length: 2, "application/x-shockwave-flash": {description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}}},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
cookieEnabled: 1,
languages: "en-US,en",
language: "en",
onLine: 1,
doNotTrack: null,
platform: "Linux x86_64",
product: "Gecko",
vendor: "Google Inc.",
vendorSub: "",
productSub: 20030107,
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
geolocation: {getCurrentPosition: function getCurrentPosition(){},watchPosition: function watchPosition(){},clearWatch: function clearWatch(){}},
javaEnabled: function javaEnabled(){return 0} };});};
var page = require('webpage').create();
page.onInitialized=function(){on_init(page)}
page.open('http://www.basketball-reference.com/players/a/abdulka01.html', function() {
    var result = page.evaluate(function() {
     return get_csv_output("advanced");
    });
    console.log(result);
    phantom.exit()
});

./phantomjs test.js >>/dev/stdout