把csv数据放到数组imacros js

时间:2015-04-12 06:58:51

标签: web-scraping imacros

我正在尝试遍历一个csv文件,将每列推入一个数组,但我不知道该怎么做,我知道标签{{!COL1}}会给我我想要的数据,但我不能弄清楚如何将它保存到我可以用来推入数组的变量中。

csvToArray = "CODE:";
csvToArray += "SET !DATASOURCE artist.csv" + "\n";
csvToArray += "SET !ERRORIGNORE YES" + "\n";
csvToArray += "SET !DATASOURCE_LINE {{CSV}}" + "\n";
csvToArray += "SET !VAR1 {{!COL1}}" + "\n";

for(i = 0; i < 10; i++){
iimSet("CSV", i);
iimPlay(csvToArray);
}

此代码允许我循环浏览csv文件,{{!COL1}}标记为我提供了我想要的数据,如何将其保存到我可以使用的varialbe中,请有人帮助不能解决这个问题。 :(

2 个答案:

答案 0 :(得分:0)

要获取COL1的数据,您需要设置EXTRACT = COL1,然后将您的变量分配给iimGetLastExtract()。

将此添加到您的宏:

csvToArray += "SET !EXTRACT {{!COL1}}";

然后将其添加到您的js文件中:

var myArray = []; // place at top of file for readability
myArray.push(iimGetLastExtract()); // place at end of for loop
                                   // puts each extraction into myArray

答案 1 :(得分:0)

尝试以下代码块。

function read_file(path) {
var content = '', i = 1, f, res = '',spl;

 while (1){
    content = res;      
    if (content != "") {
    spl = content.split("|"); 
    //spl[0] will contain the Row1 and Column1 value, spl[1] will contain Row 1 and Column 2 value etc.,
    /*Here you can specify your script/conditions
    Example:
    iimPlay('CODE:TAG POS=1 TYPE=INPUT:TEXT ATTR=NAME:login[username] CONTENT='+spl[0]);
    iimPlay('CODE:TAG POS=1 TYPE=INPUT:PASSWORD ATTR=NAME:login[password] CONTENT='+spl[1]);

I used the infinite loop, so in order to come out of the loop, you need to add a condition like below

if (spl[0]=="End") {//In your input sheet if the Row1,Col1 contains the value 'End', then it will exit the loop
        iimDisplay("All Rows Completed");
        return;
    }   

    */ 
    f = "CODE: "+"\n";
    f += "SET !EXTRACT null" + "\n"; 
    f += "SET !LOOP 3" + "\n"; 
    f += "SET !DATASOURCE \""+path+"\" "+"\n";
    f += "SET !DATASOURCE_COLUMNS 5" + "\n"; //Assume that you have five columns in the your input file
    f += "SET !DATASOURCE_LINE " + i + "\n"; 
    f += "SET !EXTRACT {{!col1}}|{{!col2}}|{{!col3}}|{{!col4}}|{{!col5}}" + "\n";
    iimPlay(f);        
    res = iimGetLastExtract();
    i++;
}
return content;
}
var file_conten = read_file("yourinputfilename.csv");