我使用CasperJS脚本作为Web服务,从节点服务器访问它。我没有做的就是让Casper成为'多线程'。如果我从邮递员同时向Casper发出两个请求,结果将在两个请求之间加扰,一个响应,第二个将为空。我看到PhantomJS有一个页面原则,但我找不到任何类似的Casper。
这是我正在谈论的casper网络服务。当我提出要求时 locahost:1338 /?query = name.name它将抓取该查询 指定的网址。当我用不同的查询发出2个并行请求时,我的问题出现了。
//includes web server modules "use strict"; var port = 1338; var server = require('webserver').create(); var url = 'url to scrap'; //start web server var service = server.listen(port, function(request, response) { var arr1 = []; var arr2 = []; var arr3 = []; var casper = require("casper").create({ verbose: true, logLevel: 'error', pageSettings: { loadImages: false, loadPlugins: false, userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36' }, clientScripts: ["vendor/jquery-1.12.1.js"] }); casper.start(url, function() { }, function(){ console.log(url + " not found"); return; }); casper.waitForSelector('.cssClass', function() { }, function(){ console.log("not found"); return; }); casper.then(function() { var query = getQuery(request.url); casper.sendKeys('.cssClass', query); casper.click('.cssClass'); casper.waitForSelector('.cssClass', function(){ arr1 = this.evaluate(function(){ var nodeList = document.querySelectorAll(".cssClass"); return Array.prototype.map.call(nodeList, function(node){ return node.textContent; }); }); }, function(){ console.log("not found"); return; }); casper.then(function(){ if(names.length > 0) { casper.waitForSelector('.cssClass', function(){ arr2 = this.evaluate(function(){ var nodeList = document.querySelectorAll(".cssClass"); return Array.prototype.map.call(nodeList, function(node){ return node.textContent; }); }); console.log("found"); }, function(){ console.log("not found"); return; }); casper.waitForSelector('.cssClass', function(){ arr3 = this.evaluate(function(){ var nodeList = document.querySelectorAll(".cssClass"); return Array.prototype.map.call(nodeList, function(node){ return node.src; }); }); console.log("found"); }, function(){ console.log("not found"); return; }); } }); }); casper.run(function() { response.statusCode = 200; response.write(JSON.stringify({p1: arr1, p2: arr2, p3: arr3})); response.close(); }); }); console.log('\nServer running at http://localhost:' + port+'/');