如何使用无头浏览器获取特定的GET请求?

时间:2015-08-23 19:07:32

标签: javascript get phantomjs headless-browser

如何通过PhantomJS或其他无头浏览器获取特定GET请求以及此请求中的信息,如https://example.de/ca.aspx?campaign=123456&pitype=Content

1 个答案:

答案 0 :(得分:0)

var Url = "http://www.google.de";
var params = new Array();
var webPage = require('webpage');
var page = webPage.create();
var targetJSON = {};
page.open(Url);
// the request url you want to catch
page.onResourceRequested = function(requestData, networkRequest) {
var match = requestData.url.match(/google.com\/test.aspx/g);
if (match != null) {
    var targetString = decodeURI(JSON.stringify(requestData.url));
    var klammerauf = targetString.indexOf("{");
    var jsonobjekt = targetString.substr(klammerauf, (targetString.indexOf("}") - klammerauf) + 1);
// request contains some JSON stuff in my case, so I decode it as a JSON object
    targetJSON = (decodeURIComponent(jsonobjekt));
    console.log(targetJSON);
    phantom.exit();
} 

};