PhantjomJS重定向原始URL请求

时间:2017-01-17 11:25:21

标签: javascript html phantomjs uri

我试图从网址http://wzdig.pbc.gov.cn:8080/dig/ui/search.action?hl=zh-CN&sr=score+desc&q=%E9%93%81&rp=&advepq=%E9%93%81&advoq=&adveq=&ext=&advtime=2&advrange=text%26title获取HTML代码,这是该网络上的特定搜索。

问题是它打印http://wzdig.pbc.gov.cn:8080/dig/ui/search.action而不是,这是进行空白搜索的结果。我做错了什么?

这是命令:

$ phantomjs get.js 'http://wzdig.pbc.gov.cn:8080/dig/ui/search.action?hl=zh-CN&sr=score+desc&q=%E9%93%81&rp=&advepq=%E9%93%81&advoq=&adveq=&ext=&advtime=2&advrange=text%26title'

和get.js:

var system = require('system');
var page = require('webpage').create();

var system = require('system');
var page = require('webpage').create();

// Imitate a real browser
page.viewportSize = { width: 1440, height: 900 };
page.settings.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

// Called every time a page is loaded
page.onLoadFinished = function(){

    // If the real page with search controls is loaded
    // then get page.content and exit
    var inputCount = page.evaluate(function(){
        return document.querySelectorAll("h3").length;
    });

    if(inputCount > 0) {
        console.log(page.content);
        phantom.exit();
    }

};

page.open(system.args[1]);

由于

1 个答案:

答案 0 :(得分:1)

不是第一次尝试,但可以获得您想要的页面 以:var page = require('webpage').create({viewportSize:{width: 1600,height: 900}, settings:{userAgent:'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0', javascriptEnabled:'true', loadImages:'false' }}), system = require('system'); page.onResourceError = function(resourceError) { console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')'); console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); }; page.onConsoleMessage = function(msg, lineNum, sourceId) { console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); }; page.onResourceTimeout = function(request) { console.log('Response (#' + request.id + '): ' + JSON.stringify(request)); }; phantom.onError = function(msg, trace) { var msgStack = ['PHANTOM ERROR: ' + msg]; if (trace && trace.length) { msgStack.push('TRACE:'); trace.forEach(function(t) { msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : '')); }); } console.error(msgStack.join('\n')); phantom.exit(1); }; page.onError = function(msg, trace) { var msgStack = ['ERROR: ' + msg]; if (trace && trace.length) { msgStack.push('TRACE:'); trace.forEach(function(t) { msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); }); } console.error(msgStack.join('\n')); }; page.onNavigationRequested = function(url, type, willNavigate, main) { console.log('Trying to navigate to: ' + url); console.log('Caused by: ' + type); console.log('Will actually navigate: ' + willNavigate); console.log('Sent from the page\'s main frame: ' + main); } page.open('http://wzdig.pbc.gov.cn:8080/dig/ui/search.action?hl=zh-CN&sr=score+desc&q=%E9%93%81&rp=&advepq=%E9%93%81&advoq=&adveq=&ext=&advtime=2&advrange=text%26title', function(){ //page.evaluate(function(){location='http://wzdig.pbc.gov.cn:8080/dig/ui/search.action?hl=zh-CN&sr=score+desc&q=%E9%93%81&rp=&advepq=%E9%93%81&advoq=&adveq=&ext=&advtime=2&advrange=text%26title'}) setTimeout(function(){ // console.log(page.content); page.render('t.png') phantom.exit(); },30000);// wait 30s });

运行此脚本
    //Camera
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
        if response {
            //access granted
        } else {

        }
    }

    //Photos
    let photos = PHPhotoLibrary.authorizationStatus()
    if photos == .notDetermined {
        PHPhotoLibrary.requestAuthorization({status in
            if status == .authorized{
                ...
            } else {}
        })
    }

该网站有问题,有时一切都差不多,但有时候不行!