【问题标题】:PhantjomJS redirecting original URL requestPhantjomJS 重定向原始 URL 请求
【发布时间】:2017-06-01 10:37:57
【问题描述】:

我正在尝试从 URL 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]);

【问题讨论】:

  • 这与bash标签有什么关系?,删除它!
  • @Igor 你是什么意思?
  • @Igor?但浏览器将其转换为 %E9%93%81 这不是假设工作。我有什么选择?

标签: javascript html phantomjs uri


【解决方案1】:

不是第一次尝试,但有可能获得您想要的页面。
将此脚本运行为:phantomjs --cookies-file=./xxx test.js >/dev/stdout

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

该网站有问题,有时几乎一切正常,但有时却不行!

【讨论】:

  • 感谢@Igor。你觉得可能是什么问题?
  • 我什至无法检测到那里的东西,我有时会看到:Trying to navigate to: http://wzdig.pbc.gov.cn:8080/L2RpZy91aS9zZWFyY2guYWN0aW9u 和:Trying to navigate to: http://wzdig.pbc.gov.cn:8080/dig/ui/search.action 而不是正确的 URL,紧随其后:Trying to navigate to: 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
猜你喜欢
  • 2012-04-15
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2020-09-25
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
相关资源
最近更新 更多