Here is a generic piece of code to extract just one column from a table into a file (extract_table_col.js)
Test-case:
----------
$imacros_dir\extract_table_col.js
Code: Select all
// --- user setup ---
var myurl='file://D:/Hello71.htm', // The url for the page with the table to extract
out_file='D:/extracted_table.csv', // Name of the Output-file for the extraction
column=1, // which column from the table should be extracted
table_ident_tag='TAG POS=1 TYPE=TABLE ATTR=TXT:* EXTRACT=TXT', // table-identifier (in case of multiple tables on the page allows to choose the right one)
out_delimiter=','; // output delimiter. For all-in-1-line use ',' For each-on-its-line use "\n", etc ..
// --- system setup ---
var nl="\n", // NewLine
RE001=/^["](.*)["]$/, // regexp to remove the first and last '"'
extract,rows,rows_count,entry,colext=column-1; // Local declarations to avoid conflicts with iMacros JS-code
// --- functions ---
// filewrite
function fwrite(
fname, // [string] filename to write to
str, // [string] string to write
append // [bool] true=append, false=overwrite
){
var fos,e;
try{
fos=new java.io.FileOutputStream(fname,append);
fos.write(java.lang.String(''+str).getBytes());
fos.close();
fos=null;
}catch(e){
alert(''+e);
return 1;
}
return 0;
};
// --- main ---
iimDisplay('Start.');
iimPlay('CODE:'+
'URL GOTO='+myurl+nl+
table_ident_tag
);
extract=iimGetLastExtract(1);
rows=extract.split("\n");extract=null;
rows_count=rows.length;
fwrite(out_file,'',false); // truncating the output-file
for(var i=0;i<rows_count;i++){
entry=RE001.exec(rows[i].split(',')[colext]);
if(entry && entry.length>=2) fwrite(out_file,entry[1]+out_delimiter,true);
}
iimDisplay('Done.'+nl+'See output-file'+nl+out_file);
D:\Hello71.htm
Code: Select all
<html>
<table>
<tr>
<td class="nowrap">AAAA-123456-BBBB</td><td>No</td><td>0</td><td class="nowrap">29-01-2008</td>
</tr><tr>
<td class="nowrap">BBST-765075-HHXN</td><td>Yes</td><td>5</td><td class="nowrap">22-02-2008</td>
</tr><tr>
<td class="nowrap">BFXJ-493794-NVHM</td><td>No</td><td>0</td><td class="nowrap">08-03-2008</td>
</tr><tr>
<td class="nowrap">BFXJ-493794-NVHW</td><td>Yes</td><td>5</td><td class="nowrap">08-03-2008</td>
</tr><tr>
<td class="nowrap">BLUL-472445-BWRW</td><td>No</td><td>0</td><td class="nowrap">27-09-2007</td>
</tr><tr>
<td class="nowrap">BLVK-782412-HMCT</td><td>Yes</td><td>5</td><td class="nowrap">03-10-2007</td>
</tr><tr>
<td class="nowrap">BLVL-472445-BWRW</td><td>Yes</td><td>5</td><td class="nowrap">28-09-2007</td>
</tr><tr>
<td class="nowrap">BPVR-026530-BCNZ</td><td>Yes</td><td>5</td><td class="nowrap">14-02-2008</td>
</tr>
</table>
</html>
[Refresh Macro List], Run the extract_table_col.js iMacro
----------
Cheers,
Joe