我的casperjs脚本有问题。我正在尝试在SQL中插入数据,但我有两条错误消息:
错误:找不到模块加密
我尝试了npm install crypto
,但这并没有改变任何事情。
我试图检索nodesjs的json以在casper中传递它,它不起作用。
第二条错误消息是:
TypeError:Object不是构造函数(评估'new Connection ({config:new ConnectionConfig(config)})“)”。
我的剧本:
var fs = require('fs');
var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: true,
loadPlugins: true,
clientScripts: ["../libs/jquery.min.js"]
},
onDie: function () {
console.log('Page is died');
},
onPageInitialized: function () {
console.log("Page Initialized");
}
});
casper.on('remote.message', function (msg) {
this.echo('remote message caught: ' + msg);
});
casper.on("page.error", function (msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
var url_connexion = 'https://accounts.zoho.eu/signin?servicename=ZohoCRM&signupurl=https://www.zoho.eu/crm/lp/signup.html';
var credentials = {email: "*******@gmail.com", password: "******"};
var identifiant = {input_email: 'input[name="lid"]', input_password: 'input[name="pwd"]', valider_connexion: '#signin_submit'};
var result = [];
var x = require('casper').selectXPath;
casper.start(url_connexion, function() {
console.log('start1');
console.log("*************************");
console.log(" Page de connexion chargee");
console.log("*************************");
casper.wait(5000, function(){
casper.capture("./screenshots/step_1.png");
console.log('premier_screen')});
this.sendKeys(identifiant.input_email, credentials.email);
this.sendKeys(identifiant.input_password, credentials.password);
casper.wait(5000, function(){
casper.capture("./screenshots/step_2.png");
console.log('second_screen')});
casper.thenClick(identifiant.valider_connexion, function(){
casper.wait(5000, function(){
casper.capture("./screenshots/step_3.png");
console.log('troisieme_screen');
});
});
});
casper.then(function(){
console.log('ATTENTIONNNNNNNE');
var myparameters = {Url:"https://crm.zoho.com/crm/ShowTab.do?module=Invoices"};
casper.wait(5000, function(){
casper.thenOpen('https://crm.zoho.com/crm/ShowTab.do?module=Invoices', function(){
console.log("step_cap");
casper.capture("./screenshots/step_4.png");
});
});
});
casper.then(function(){
console.log('NEWWWW');
var myparameters = {Url:"https://crm.zoho.com/crm/EntityInfo.do?module=Invoices&id=2957002000000136328"};
casper.wait(5000, function(){
casper.thenOpen('https://crm.zoho.com/crm/EntityInfo.do?module=Invoices&id=2957002000000136328', function(){
console.log("step_cap");
casper.capture("./screenshots/step_5.png");
casper.then(Extract_data_on_page);
});
});
});
function Extract_data_on_page(){
console.log('extract_data');
var zoho_result_identifiant =
{
result_id:'//*[@id="subvalue_INVOICENUMBER"]'
};
var result_intermediaire = {name :""};
result_intermediaire.name = this.fetchText('tr#detailViewButtonLayerDummyTableRow #subvalue_INVOICESUBJECT');
result= [];
result.push(result_intermediaire);
casper.then(function(){
console.log('***********************');
console.log(result[0].name);
console.log(result[0]);
console.log(result);
console.log(result_intermediaire);
console.log('***********************');
});
}
casper.then(function(){
var mysql = require('mysql');
console.log("start_bdd");
var con = mysql.createConnection({
user: 'root',
password: '*****',
host: 'localhost',
database: 'zoho_first'
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO Factures (Titre) VALUES ('Company Inc')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
});
casper.run();
答案 0 :(得分:0)
CasperJS和PhantomJS与node.js不兼容(尽管某些 npm包可能如果他们不依赖于节点的内部模块并且不使用ES6)。
因此,您无法从CasperJS将数据直接保存到MySQL中。幸运的是有其他选择:
您可以使用您知道的语言构建一种中间人脚本,该脚本可以从CasperJS接收数据并将其放入MySQL中。您可以通过HTTP或CLI向其发送数据。例如,它可以是localhost上等待POST请求的简单PHP脚本。
您可以使用PhantomJS fs
模块以SQL格式保存数据,然后生成MySQL进程以通过child process模块