在javascript iMacros中使用SET!VAR1 EVAL

时间:2015-10-17 10:52:58

标签: javascript imacros data-extraction

我有一个.iim iMacros文件,用于从网站中提取文本,它看起来像这样:

SET !DATASOURCE listofurls.csv
SET !DATASOURCE_LINE {{CSV}}
URL GOTO={{!COL1}}
WAIT SECONDS=1
TAG POS=1 TYPE=PRE ATTR=TXT:* EXTRACT=TXT
SET !VAR1 EVAL("var a=\"{{!EXTRACT}}\"; var b=a.indexOf(\"total\"); var c=parseFloat(a.substring((b+9),(b+9+3))); c")
SET !EXTRACT NULL
SET !EXTRACT {{!VAR1}}
SAVEAS TYPE=EXTRACT FOLDER=* FILE=result.csv

它在Play(循环)模式下运行良好,但需要自动运行作为循环从shell脚本自动启动它。作为一个正常的.iim文件不能自动作为循环我试图将其转换为iMacros javascript:

var accounts = 10;
for(i = 1; i <= accounts; i++){

iimDisplay("Current loop: "+ i);

var extract;
extract =  "CODE:";
extract +=  "SET !DATASOURCE followersapi.csv" + "\n"; 
extract +=  "SET !DATASOURCE_LINE {{CSV}}" + "\n"; 
extract +=  "URL GOTO={{!COL1}}" + "\n"; 
extract +=  "WAIT SECONDS=2" + "\n"; 
extract +=  "TAG POS=1 TYPE=PRE ATTR=TXT:* EXTRACT=TXT" + "\n"; 
extract +=  "SET !VAR1 EVAL("var a=\"{{!EXTRACT}}\"; var b=a.indexOf(\"total\"); var c=parseFloat(a.substring((b+9),(b+9+3))); c")" + "\n"; 
extract +=  "SET !EXTRACT NULL" + "\n"; 
extract +=  "SET !EXTRACT {{!VAR1}}" + "\n"; 
extract +=  "SAVEAS TYPE=EXTRACT FOLDER=* FILE=result.csv" + "\n"; 

iimSet("CSV", i);
iimPlay(extract);

}

我收到此错误:

  

SyntaxError:missing;在陈述之前,第23行(错误代码:-991)

经过一些研究后,修改了这一行:

SET !VAR1 EVAL("var a=\"{{!EXTRACT}}\"; var b=a.indexOf(\"total\"); var c=parseFloat(a.substring((b+9),(b+9+3))); c")

到此:

SET !VAR1 EVAL(\"var a=\'{{!EXTRACT}}\'; var b=a.indexOf(\"total\"); var c=parseFloat(a.substring((b+9),(b+9+3))); c\)

然后我收到了这个错误:

  

SET命令格式错误,第9行(错误代码:910)

关于如何让这个工作的任何想法?我也尝试从.js文件启动.iim文件并从那里循环它,但是然后!LOOP总是1并且它总是读取csv的第一行..所以它很没用。 / p>

非常感谢!

1 个答案:

答案 0 :(得分:2)

尝试这种方式:

extract +=  'SET !VAR1 EVAL("var a=\'{{!EXTRACT}}\'; var b=a.indexOf(\'total\'); var c=parseFloat(a.substring((b+9),(b+9+3))); c")' + "\n";