問題是解決了,現在它的工作原理:
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
能否請你解釋一下你怎麼知道做出這些變化,以及爲什麼他們有必要嗎? – Mahir
是的,我們需要至少改變'UserAgent',以使這個腳本起作用。隨着我所做的更改,您將看到一個虛假的導航器對象,看起來像一個普通的瀏覽器。 – 2016-11-10 01:11:49
本例中導航器對象的擴展版本: \t http://pastebin.com/kSndS8jX – 2016-11-10 01:16:44