Data Extraction

From iMacros
Revision as of 13:20, 5 August 2021 by Td (talk | contribs) (→‎Extraction Wizard iMacros Browser IE Plug-in)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Data Extraction and Web Scraping iMacros Browser IE Plug-in Firefox Chrome iMacros 2021.0

A key activity in web automation is the extraction of data from websites, also known as web scraping or screen scraping. Whether it is price lists, stock information, financial data or any other type of data, iMacros can extract this data for you and either re-use the data or store it in a file or database.

iMacros can write extracted data to standard text files, including the comma separated value (.csv) format, readable by spreadsheet processing packages. Also, iMacros can make use of the powerful scripting interface to save data directly to databases.

The Extract command

Data extraction is specified by an EXTRACT parameter in the TAG command. This parameter replaces the usual CONTENT parameter. Please see the updated Demo-Extract for some examples of this, including the following:


This means that the syntax of the command is now the same as for the TAG command, with the type of extraction specified by the additional EXTRACT parameter.

Creation of Extraction Tags

Extraction Wizard iMacros Browser IE Plug-in

Text Extraction Wizard

The Extraction Wizard can be used to automatically generate and test extractions.

To define an EXTRACT command proceed as follows:

  • Whilst in record mode, open the Text Extraction Wizard ("Text" button on the Rec tab).
  • In the browser window or frame select the text that you want to extract.
  • Choose what type of extraction you want to perform on that element, like TXT, HTM, HREF, ALT, TXTALL, or TITLE. Not all types are available for all elements.
  • The extracted information will be displayed in the wizard. iMacros also creates a suggestion for the tag command attribute and position.
  • If the result is #EANF# (Extraction Anchor Not Found) you will need to alter the extraction anchor in order to successfully extract the data.
  • If you are satisfied with the result click "Add Command" to add a TAG command with the EXTRACT statement to the macro.

Table extract commands can be easily produced and checked using the Text Extract Wizard. If the element chosen is a table, the table data is properly formatted and displayed in the wizard.

iMacros Browser Note: The extraction wizard is only available in the iMacros Browser and iMacros for Internet Explorer But the generated commands can be used in all iMacros versions.

iMacros 2021.0 Note: The extraction wizard is currently not available in iMacros 2021. Please use Manual Creation in the meantime.

Extraction from Framed Websites

If the information you want to extract is inside a framed website you need to have a FRAME command to mark the frame as active for extraction.

When recording a TAG command the FRAME command will automatically be generated.

URL GOTO=       
FRAME F=5     

Within the Extraction Wizard, when selecting the data to be extracted the FRAME command will automatically be generated.

URL GOTO=       
FRAME F=3     

Manual Creation iMacros Browser IE Plug-in Firefox Chrome iMacros 2021.0

In order to manually create an extraction tag, it is necessary to first record a TAG command. In record mode, click on the data to be extracted. After stopping the macro recording, open the macro for editing and replace the CONTENT= attribute with the EXTRACT=TXT parameter (or just simply add the EXTRACT parameter to the end of the command if the CONTENT parameter does not exist). If you need to extract other information than text you can use the TXTALL, HTM, HREF, TITLE, ALT or CHECKED attribute instead of TXT.


Starting with iMacros 10.2, you can extract any attribute. If you want to know which attributes are available, record the TAG command in Expert Mode.


Normally, the tooltips are specified by the TITLE attribute of an element. You can extract the tooltip content with iMacros:


The qTip tooltip plugin for the jQuery JavaScript framework uses not the title attribute but a custom attribute named "data-qtip":


ComputerName=* is the initial immutable part of the tooltip content and is used to specify which tooltip should be extracted. Recording in Expert Mode will create a TAG command that consists of all attributes of the clicked HTML element.

Extract Complete Website iMacros Browser IE Plug-in Firefox Chrome iMacros 2021.0

To extract a complete web page (or the complete header or body) you need to manually insert the appropriate TAG line. Please see the examples:

 'Complete Page
 'Complete Page TEXT only
 'Page header only  
 'Page body

Alternatively, you can use the SAVEAS command to save the complete web page.

For an example using the SEARCH command, please see the following forum post: Most Efficient Way To Extract Source Code

Extract Table iMacros Browser IE Plug-in Firefox iMacros 2021.0

Use TAG TYPE=TABLE ... to extract the content of a complete table with one command. Example: Demo-Extract-Table.

This method works well with simple, well-formatted tables. For more tricky table extractions you always have the option to extract them cell by cell as shown in the !ENDOFPAGE example.

Table cells in the extracted data are separated by the string #NEXT# and table rows are delimited by the string #NEWLINE#. These tags are automatically translated into commas and newlines when you use the "SAVEAS TYPE=EXTRACT" command, but the delimiters are retained when returning the data to a script via a call to iimGetExtract.

If the table you are attempting to extract also contains nested tables, then the inner table data will also be separated by commas and new lines (in CSV format, via SAVEAS), or #NEXT# and #NEWLINE# (via iimGetExtract).

You can use the Text Extraction Wizard to see the resulting extracted table, but in this case, for visual simplicity, the inner tables are shown as plain text, without the delimiters.

Table extraction with the Text Extraction Wizard

Extract Page Title iMacros Browser IE Plug-in Firefox iMacros 2021.0

To extract a title of a website you need to manually insert the appropriate TAG line with TYPE=TITLE. This TAG command finds the page's title element. Please see the example:


Extract Page URL iMacros Browser IE Plug-in Firefox iMacros 2021.0

To extract the URL of a website as shown in the browser address bar please use the built-in !URLCURRENT variable and store this value in !EXTRACT with the SET or ADD command.


Test Popup

When manually running a macro with an extraction TAG, by default the extraction will be displayed on the screen. This facility can be switched off using the following command:


iMacros 2021.0 Note: Default value is set to NO in iMacros 2021.

Handling Extraction Results

If in one macro several EXTRACT commands appear then the results are separated by the string [EXTRACT].

iMacros 2021.0 Note: The extracted data is also shown in the Logs panel in iMacros 2021.


You can save extracted data directly to a CSV file by adding a "SAVEAS TYPE=EXTRACT" command manually to the macro. All items that were extracted before the SAVEAS command are saved to the specified file in one row like

"item1", "item2", "item 3", ... 

As you can see the [EXTRACT] tags, which are inserted to distinguish results from different EXTRACT commands, are substituted by commas. If in the Options dialog you have checked "Use regional settings in CSV files", the "comma" between each extraction is going to be your system list separator (a semi-colon ";" for instance) instead of ",".

The SAVEAS command erases the content of the !EXTRACT variable afterward. With the next start of the macro or the next round of a loop a new line is added to the file.

Saving arbitrary values

In addition to extracting data from web pages, you can also save any arbitrary value to your output file by adding it to the extraction buffer with the following syntax:

ADD !EXTRACT <value>

Where <value> is replaced with the actual value you want to save (can also be an EVAL expression). For example, the following will add a timestamp column to the output:

ADD !EXTRACT {{!NOW:yyyy/mm/dd_hhnn}}

Each time you add something to !EXTRACT either by using the ADD command or as part of a TAG...EXTRACT command, it will save that value as a separate column when using SAVEAS TYPE=EXTRACT.

Extraction & the Scripting Interface

(Related example scripts: Extract-and-fill.vbs, Extract-2-file.vbs, Get-Exchange-Rate.vbs)

All extracted data can be sent to your code via the Scripting Interface. This gives you all the power of any programming language you choose to process the extracted information further or simply save it to a file.

Use the iimGetLastExtract command to return the extracted information from the macro.

The extracted text is returned as a string. Extracted information resulting from different extractions are separated by [EXTRACT], e.g.

Text to be extracted[EXTRACT]
Salary: 33,000.00 per year[EXTRACT]... 

Remember: Using the "SAVEAS TYPE=EXTRACT" command will reset the contents of the !EXTRACT variable. Thus, using this command in a macro whose extraction result you wish to obtain via the Scripting Interface will result in an empty string in your application!

If you extract a complete table the data from different columns is separated by #NEXT# and each table row ends with #NEWLINE#. You can easily use the separation tags to split the complete dataset. In Visual Basic Script this would, for example, look something like:

s = Replace(s, "#NEWLINE#", """" + vbCrLf + """") 
s = Replace(s, "#NEXT#", """"+ "," + """") 

Related forum post: Missing #NEXT# delimiters in .csv from web extraction

Example 1 - Transfer extracted values to calling program

Use iimGetLastExtract to retrieve the values.

 iplay = iim1.iimPlay("wsh-extract-rate")
 If iplay = 1 Then
      s = "One US$ costs " + iim1.iimGetLastExtract(1) + " EURO or " + iim1.iimGetLastExtract(2) + " British Pounds (GBP)"
    s = "The following error occurred: " + iim1.iimGetLastError()
 End If

Unsuccessful Extraction

If the extraction was unsuccessful, i.e. the extraction anchor could not be found on the page, the !EXTRACT variable holds the string #EANF# (Extraction Anchor Not Found). However, the return value that informs you whether the execution of a macro was successful is still positive (iimPlay = 1). The reason for this behavior is that a macro can have many TAG...EXTRACT commands and often only one or a few of them do not find the extraction anchor. If you want to check if a particular EXTRACT command was successful you just need to check if #EANF# is present in the returned string. Often this can be very useful, for example, if you use EXTRACT to check if a keyword is present on a page. A returned string containing #EANF# indicates that the keyword is not found. For comparison, if a standard TAG command cannot locate the defined element then iMacros returns an error.

Image Extraction

The Image Extraction Wizard helps you the create the right commands TAG... CONTENT=EVENT:SAVEITEM for image web scraping. Please see the Save Web Page Elements chapter for more details.

Extraction of Dialog Text

To get the text of a dialog use


in the macro at any position before dialog appears. Now the content of a dialog is added to the extracted text, i.e. to the !EXTRACT variable.



The PROMPT command in this example is simply used to show the extracted values. The WAIT statement is not directly required, but there has to be a 1-2 seconds delay between the time you trigger the dialog and the first time you use the extracted dialog text. The reason for this is that there is a small delay between the time the TAG command triggers the dialog (e. g. by clicking on a link) and the time the dialog actually appears. iMacros has no way of knowing beforehand that a certain link will trigger a dialog. So it has to "catch" the dialog once it appears and then handle it. Typically this whole process is fast and takes less than a second, but until it is complete the !EXTRACT variable is not filled with the text from the dialog.


Extracting From SELECT Elements

In HTML code drop-down lists are generated by a SELECT tag.

With a simple EXTRACT=TXT, the currently active value is extracted:


In order to extract all options in a drop-down list use


!EXTRACT will contain the complete list of entries, separated by the keyword [OPTION]

Related forum posts: How to extract value from option tag instead of text

Extraction and the PRE Tag

Some web pages make use of a <PRE ...> tag in their HTML code. It marks the enclosed text as preformatted -- all the spaces and carriage returns are rendered exactly as you type them. The information enclosed in a <PRE> tag is extracted correctly (including the formatting!) by iMacros. Thus, if you transfer the extracted data via the Scripting Interface all formatting information is retained unchanged. The formatting is only changed on two occasions: line breaks are removed when displaying the result in the test dialog box and when saving the result using the SAVEAS command. This is necessary to ensure proper formatting of the CSV formatted text file because in the CSV format a line break would start a new line.

Extract with Relative Positioning

(Related example macro: Demo-ExtractRelative )

Relative Positioning with the iMacros Browser and iMacros for IE

When extracting data from a complex website the extraction can be made easier if you can tell iMacros to start the search for the extraction anchor after a specific point on the page (as opposed to starting from the top, which is the default).

Extract the current gold price (sell/buy) given the item number. We use relative extraction to get the prices in the line with the correct item number.

For example, assume you want to extract data from a specific cell in a table, in this case, the size of the land in the second table.

Table 1 - Relative Extraction
Table 2 - Relative Extraction

Without relative positioning, you would have to count the cell from the top of the page including cells from other tables that come before the land table. Although the extraction wizard can do this for you, you run into problems as soon as the number of rows in a table are not constant as they are in the above example. The Transfer table of result 1 has four rows, that of result 2 has five rows. Thus, an absolute position parameter like so


will potentially result in the extraction of an unwanted result.

With relative positioning you tell iMacros to search for the extraction anchor located after the position that is indicated by a TAG command immediately before your TAG...EXTRACT command. In our case we click on the table title "Land" before starting the extraction wizard to create a TAG command. Note that this TAG command does not click on any link, rather it only marks an element to indicate a position for the following TAG command. Relative positions are indicated with an R before the position number.

  • If you want to use a button or a link as reference, you should tag it with TAG ... EXTRACT=TXT, to avoid following the link or "pushing" the button. In that case, do not forget to use SET !EXTRACT NULL, to clear the extract variable before the real extract.

How to limit the extraction search range

Use !ENDOFPAGE to limit the extraction to a range above a certain trigger word or image.

Extracting data from with Relative Positioning. In this example, we use the word "Price" to find the book price. This creates very stable web-scraping macros.

Backwards relative positions

Since iMacros V6.20 you can also indicate backward positions (to the left and/or top of a selected element). This negative relative extraction supports up to 10 backwards steps (POS=R-10).

 'Negative positioning => move to the LEFT and/or TOP of the anchor
 TAG POS=1 TYPE=TD ATTR=TXT:31023G20080822
Negative (backward) Positioning. The task is to select the checkbox to the left of a given order-ID. Since the select box itself has no name, we use the ID in the second column to find the checkbox.

How to skip a missing value

If you use relative extraction and a certain data record (e. g. a phone number) is missing on a page, then the macro would normally stop with a TAG error as the TAG for the anchor fails. But that is not what you want during an extraction: You simply want the macro to continue and extract all other values that exist. => Solution: Add SET !ERRORIGNORE YES.

Note that when the anchor TAG immediately before a relative extraction fails, then the extraction itself also fails (= returns #eanf#). This is by design to make sure that iMacros extracts only the intended value (if the extraction anchor exists) or no value ("#eanf#") if the extraction anchor is not found.


 URL GOTO=     
 'Correct:  TAG POS=1 TYPE=DIV ATTR=TXT:MyTable

OTHERNAME was added to the TXT:MyTable attribute to trigger the extraction anchor failure for demo purposes.

Relative Positioning with iMacros 2021, iMacros for Chrome and Firefox

In a nutshell, the principle is the the same as described above for the iMacros browser/IE, except the position is relative to the end (close tag) of the anchor element. Consequently, it is not possible to tag an element relative to its outer element in a nested situation.

This is easier to understand with a simple example. The following macro first tags the table cell TD and looks for the next occurrence of a link.

TAG POS=1 TYPE=TD ATTR=CLASS:bdytxt&&TXT:HREF<SP>extracts<SP>links

When played in the iMacros browser or iMacros for IE, this macro extracts the text of the link which is inside that table cell, namely "HREF extracts links". iMacros for Chrome and Firefox extract the next link's text: "keyword detection with EXTRACT".

How to convert relative positioning for Chrome and Firefox

Knowing that relative positioning is relative to the element's end, all you have to do is change the anchor (reference) tag. For instance, this macro for extracting the values in each table row using the iMacros browser/IE:

 TAB T=1
 'Anchor to the exchange rates table
 'Extract each of the rows *inside* the table

can be easily adapted to:

 TAB T=1
 'Anchor to the header row
 'Extract each of the rows *below* the header

Notice that both produce the same result, but in the first version !LOOP has to run from 3 to 7, while the new version needs to !LOOP from 1 to 5 to get all the rows in the table.


Alternatively, you can use XPATH to position the tag. All versions of iMacros also support XPath expressions in the TAG command.

Related forum posts

Asian Language Support

iMacros runs on all language version of Windows, including the so-called "double-byte" languages like Chinese, Japanese or Korean.

Asian Languages Text Extraction:

iMacros and the Scripting Interface include full Unicode support, so you can extract Asian language characters (e.g. Japanese) even on Western Windows versions (e.g. English).

For more details please see these forum posts:

See Also