简体   繁体   中英

Calling function with phantomjs gives different result than calling from console

I'm trying to get NBA player stats from this page . There is a UI button that allows you to convert the data tables to csv, and I'm trying to automate this process. Under the hood, it's calling the function get_csv_output() .

In the inspector console, get_csv_output("per_game") and get_csv_output("advanced") outputs the #per_game and #advanced tables, respectively, in csv format.

However, when I try calling the get_csv_output() function using phantom.js, it only fetches the csv data for the "per_game" table, but doesn't work for the "advanced" table.

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()
});

The output of this is the per_game table in csv format as expected. However, when I try changing it to get_csv_output("advanced") ,

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

I tried providing some of the other table ids as input, and per_game appears to be the only one that works.

The problem is solved, now it works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM