Data Extraction

From iMacros
Revision as of 10:59, 26 April 2007 by Hm (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Extract single elements

Extraction Wizard

(Related example macro: Demo-Extract, Demo-ExtractRelative)

iMacros can extract data from Web sites [iMacros Browser only]. Click on the Extract Data button while in record mode to bring up the extraction wizard that will help you create the correct EXTRACT command:

Note: Internet Explorer 6.0 or better must be installed in order to use the EXTRACT command.

The EXTRACT command and thus the extraction is controlled by three different parameters: the extraction anchor, the position and the type of extraction. The most important parameter is the extraction anchor. It contains information on the HTML code around the information which is to be extracted. You must use * at the end of the extraction anchor. If the HTML code given in the anchor appears more than once on a page the position parameter determines which of the occurences is extracted. The type of extraction determines if the result is plain text, HTML source code, an URL, an element's title or the alternative text of an image.

All extraction results can be accessed inside the macro through the built-in variable !EXTRACT. If this variable contains #EANF# (Extraction Anchor Not Found) the extraction was unsuccessful.

Results of multiple extractions in the same macro are separated by a [EXTRACT] tag in the !EXTRACT variable.

During manual replay of macros including EXTRACT commands in the iMacros Browser the extraction result is displayed in a dialog window by default. This behaviour can be controlled by setting the built-in !EXTRACT_TEST_POPUP variable.

Some Background on HTML and Extraction

HTML is the language in which web sites are coded. The language consists of so-called tags, which determine how elements are formated, displayed and aligned. Each HTML tag consists of two parts, an opening part and a closing part. All text between the opening and closing tags is affected by the directives the HTML tag implies. E.g. the following HTML snippet

This text is <B>bold</B> 

will result in

This text is bold 

i.e. the B tag is used to format text in bold face. When extracting text with iMacros the following procedure is applied:

  • iMacros searches the HTML source of the currently active webpage for an occurence of the extraction anchor
  • If the anchor is found all text between the opening HTML tag of the anchor and its equivalent closing tag is extracted
  • If the anchor is not found the result is #EANF#

Create Extraction Command

Open Wizard
Create EXTRACT tag
Test EXTRACT command
Add EXTRACT tag to macro

To define an EXTRACT command proceed as follows:

  • Open the Extraction Wizard ( "Extract Data" button on the Rec tab of the control panel).
Note: If the information you want to extract is inside a framed web site you need to click inside the frame that contains the information you want to extract before opening the Extraction Wizard. This generates the FRAME command and marks the frame as active for the extraction.
  • In the browser window or frame select the text that you want to extract.
  • Click the "Suggest EXTRACT Tag" button. The marked information will be displayed in the yellowish text area on the left. iMacros also creates a suggestion for the extraction anchor, which is display in the orange text field on the right.
  • Click "Test EXTRACT Tag" to test run the extraction tag. The result of the generated extraction anchor will then be displayed in the yellow text area on the right hand side of the wizard. 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 this EXTRACT tag" to add the EXTRACT statement to the macro.

Save Extraction Result

There are two methods to retrieve extracted data.

SAVEAS

You can save extracted data directly to a 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 are substituted by commas. The SAVEAS command erases the content of the !EXTRACT variable afterwards. With the next start of the macro or the next round of a loop a new line is added to the file.

iimGetLastExtract()

You can also use the iimGetLastExtract() method of the Scripting Interface to access the extracted data in your application. Potential [EXTRACT] tags are included in the returned string and can be used to separate different extraction results.

Unsuccessful Extraction

As said above, 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 (usually 2). The reason for this behaviour is that a macro can have many 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.

Extraction of Dialog Text

To get the text of a dialog use

SET !EXTRACTDIALOG YES 

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

Extracting From SELECT Elements

In HTML code drop down lists are generated by a SELECT tag. For SELECT boxes the currently active value is extracted. If you want to select all values of a drop down list manually add "#ALL#" before the extraction anchor: Select currently active values:

EXTRACT POS=1 TYPE=TXT ATTR=<SELECT<SP>size=1<SP>name=main>* 

Select all values in a list:

EXTRACT POS=1 TYPE=TXT  ATTR=#ALL#<SELECT<SP>size=1<SP>name=main>* 

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.

Trouble Shooting

Test EXTRACT command

Sometimes iMacros cannot suggest a proper extraction anchor automatically, in which case you can create one manually. Enter it in the orange text area on the right side of the extraction wizard and test it with the "Test EXTRACT tag" button. Please read all the information in this Chapter to get a good overview of how the EXTRACT command can be tweaked manually.

The POS Parameter

This example is intended to shed some light on the use of the POS parameter of the EXTRACT command. Consider the following HTML source code from which you would like to extract the 'Text to be extracted' part:

<B>Hello World</B> 
<B>Text to be extracted</B> 
<B>Good Morning</B> 
<B>Good Afternoon</B> 
<B>(c) iOpus</B> 

The extraction anchor is clearly <B>* because the information is enclosed by a B tag. However, on this page this anchor can potentially match all 5 texts and we cannot further indicate which text to extract by tweaking the extraction anchor. But we can tell iMacros which occurence of the extraction anchor to extract. Hence the correct EXTRACT command is:

EXTRACT POS=2 TYPE=TXT ATTR=<B>*   

As said before you must use * at the end of the extraction anchor to tell iMacros that it should ignore the rest of the elements when searching for the anchor.

Starting with Version V4.30 you can also use relative positioning for the extraction anchor.

Extract with relative Positioning

(Related example macro: Demo-ExtractRelative )

When extracting data from a complex websites 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 start from the top, which is the default).

E.g., 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

EXTRACT POS=8 TYPE=TXT ATTR=<TD>*    

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 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 EXTRACT command. Relative positions are indicated with an R before the position number.

TAG POS=1 TYPE=B ATTR=TXT:Land 
EXTRACT POS=R1 TYPE=TXT ATTR=<TD>*

Using wildcards

The extraction anchor must end with the wildcard character *. However, you can also use the wildcard within the extraction anchor. Consider the following HTML code and say, you want to extract the salary.

<li> 
  <nobr> 
    <font face="Verdana" size="-1"> 
      <b>Salary:</b>33,000.00 per year 
    </font> 
  </nobr> 
</li> 

One extraction anchor would for example be

EXTRACT POS=1 TYPE=TXT ATTR=<FONT<SP>face=Verdana<SP>size=-1><B>Salary:</B>*

In this example you can now substitute any parts of the anchor with the wildcard. The reason for doing this might be that the web site changes from time to time or you just need to have a more compact extraction anchor.

Suppose the font face changes every now and then, resulting in some unsuccessful extraction. You can easily change the anchor to

EXTRACT POS=1 TYPE=TXT ATTR=<FONT<SP>face=*<SP>size=-1><B>Salary:</B>*   

If the size also changes you can change it to

EXTRACT POS=1 TYPE=TXT ATTR=<FONT<SP>face=*<SP>size=*><B>Salary:</B>*   

or even

EXTRACT POS=1 TYPE=TXT ATTR=<FONT*><B>Salary:</B>*   

If the extraction is successful the extract text is "Salary: 33,000.00 per year".

Example: Keyword Anchor

(Related example macro: Demo-Extract)

Often you just want to extract information connected to a certain word on a web site. In this case you can use a so-called keyword anchor, which is nothing else than the cunning use of wildcards.

In this example we would like to extract the information about the appearance of an object. As you can see from the example this information is always connected with the word "Appeared".

FSBO 4-2-2, $110 KCHAPEL CREEK 111-222-3333 
Fort Worth West Sale 
First Appeared in the FW Newspaper 
FSBO NICE AREA4-2.5-2 2243SF, $139KMSID 222-555-9879 
Arlington Southwest Sale 
First Appeared in ABC Star 
SALE/LEASE condo, 3-2-2 enclosed patio comm. pool SW FW in Villas on the Bluff a gated comm. FSBO $169.5k 555-1111-7997 
Condo/Townhome/Apt/DuplexSale 
First Appeared in the Star-Telegram 

Using the Extraction Wizard, it would suggest a very general extract command, e.g.:

EXTRACT POS=29 TYPE=TXT ATTR=<TD>* 

While this might work, it is sensitive to small changes in the web page layout. If another table cell is inserted before this one you will extract the wrong data. In this case, however, you can easily fine tune your extraction to make it more robust against web page changes using the keyword "Appeared" and wildcards:

EXTRACT POS=1 TYPE=TXT ATTR=<TD>*Appeared*  

Remember to reset the POS attribute since, with the "Appeared" part, it is the first occurence.

Example: Data separated with <br>

(Related example macro: Demo-Extract)

Sometimes you have line breaks in extracted information inflicted by the HTML tag
like so:

<table> 
  <tr> 
    <td> 
      John Smith<br>Main Street<br>Arlington 
    </td> 
  </tr> 
</table> 

On the web page this might look like:

John Smith 
Main Street 
Arlington 

You can only create one extraction tag for this line, i.e.

EXTRACT POS=1 TYPE=TXT ATTR=<TD>* 

So you can not create a separate extraction commands for name, street and city because these informations are not enclosed by separate opening and closing HTML tags. With the default TYPE=TXT extraction, all parameters would be extracted into one line and are difficult to separate. The result would be

John Smith Main Street Arlington 

To work around this problem use the "TYPE=HTM" extraction. It preserves all HTML tags inside the text so that the extraction result is:

John Smith<br>Main Street<br>Arlington 

This result can be further processed and split with any programming or scripting language using the Scripting Interface. For example, in Visual Basic Script you can use the Split function:

MyArray = Split(extracted_string, "<br>") 

"MyArray" will now have three elements: "John Smith", "Main Street" and "Arlington".

Extract complete tables

(Related example macro: Demo-Extract-Table )

To extract a complete table with only one command you can use

EXTRACT POS=3 TYPE=TXT ATTR=<TABLE*   

The !EXTRACT variable now contains the entire information of the table. And iMacros has done more! It has put a #NEXT# tag between adjacent table elements and a #NEWLINE# tag at the end of every table row. These tags are automatically translated into commas and newlines when you use the "SAVEAS TYPE=EXTRACT" command, such that the following table

Order # Item Price (US$)
331-445 Book 29.95
444-555 CD-ROM 15.00


will, when using this command

SAVEAS TYPE=EXTRACT FOLDER=* FILE=* 

be saved in a CSV conform file looking like this

Order#  , Item , Price (US$) 
331-445 , Book , 29.95 
444-555 , CD-ROM, 15.00  

By default a comma (",") is used as separator. This can be modified by changing the entry for "CSVcomma=," in the iMacros settings file (File name: "iim.ini").

If you access the extracted information via the Scripting Interface you can easily use the separation tags to split the complete dataset.

Extract complete website

Since in the HTML language all web sites are enclosed by the HTML tag, you can extract a complete website including all HTML tags using

EXTRACT POS=1 TYPE=HTM  ATTR=<HTML*   

This can be very useful if you need to do your own parsing of the web site's contents.

To return only the body (main content) of a web page you can use

EXTRACT POS=1 TYPE=HTM  ATTR=<BODY*   

Save extracted data

(Related example macros: Demo-Extract, Demo-Extract-Table)

There are two methods to retrieve extracted data.

SAVEAS (PRO and SCRIPTING Edition)

You can save extracted data directly to a 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. The SAVEAS command erases the content of the !EXTRACT variable afterwards. With the next start of the macro or the next round of a loop, a new line is added to the file.

iimGetLastExtract() (SCRIPTING Edition)

You can also use the iimGetLastExtract() method of the Scripting Interface to access the extracted data in your application. Potential [EXTRACT] tags are included in the returned string and can be used to separate different extraction results - see the included extract-2-database.vbs.

Extract & 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 text if you used any EXTRACT commands within the macro.

The extracted text is returned as a string. Extracted information resulting from different EXTRACT commands 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#", """"+ "," + """") 


Example 1 - Split the returned string

The returned string is split to separate the results from different EXTRACT commands.

Dim data as String 
Dim s as String 
Dim ExchangeRate 
iplay = iim1.iimPlay("wsh-extract") 
If iplay = 1 Then  
  data = iim1.iimGetLastExtract() 
  ExchangeRate= Split(data, "[EXTRACT]") 
  s = "One US$ costs " + ExchangeRate(0) + " EURO or " + ExchangeRate(1) + " British Pounds (GBP)" 
  MsgBox s 
End If 


Example 2 - Keyword search

We want to find out if the word "iopus" exists on a web page. If yes, then print the page. To make this example work, create the following macro and save it under the filename "mysearch.iim" in your Macros directory:

VERSION BUILD=3301125 
'The keyword *is* the data extraction anchor! 
EXTRACT POS=1 TYPE=TXT ATTR=*iopus*      

To print the web page create the following macro and save it under the filename "print_this.iim" in your Macros directory:

VERSION BUILD=3301125 
PRINT     

Use the following Windows Script to control the macros:

set iim1= CreateObject ("InternetMacros.iim") 
iret = iim1.iimInit() 
iplay = iim1.iimPlay("mysearch") 
extracted_text = iim1.iimGetLastExtract() 
'test if keyword appeared on website.  
If iplay = 1 Then  
   if instr (extracted_text, "#EANF#") > 0 then 
          MsgBox ("Sorry, keyword not found") 
   else 
         iplay = iim1.iimPlay("print_this") 
   End If 
End if 
If iplay < 0 Then 
       MsgBox "Error!" 
End If    

Note: You can also write directly to any Windows database. Please see the "extract-2-database.vbs" script for some example code. The script writes all results directly to a Microsoft Access database.

More Examples

iMacros comes with several example scripts that demonstrate the EXTRACT command:

  • extract-2-file.vbs
  • extract-and-fill.vbs
  • get-exchange-rate.vbs

The scripts are found in the "Examples\Windows Scripting Host" directory of your iMacros installation. More example scripts and test pages are available at http://www.iOpus.com/iim/demo

Extract Tech Tip

Question: EXTRACT works while I am testing in the Extraction Wizard, but when I run the macro Extract only returns #EANF# (Extraction Anchor not found).

Answer: Some websites are created dynamically from databases and the exact content of the website changes every time you visit a page. The solution is to replace the changing part of a link or extraction with the wildcard symbol.

Example: Assume you searched for a product on a retailers site and the resulting page is a table of products, each with its own description and price tag, which are enclosed by the A HTML tag, like so:

 <TR> 
   <TD> 
     <A class=price href="/homes/homesforsale/view_details.jsp?advertID=14470882&listID=2492&index=1&amp"> 
       Product 1, Price 1 
     </A> 
   </TD> 
 </TR> 
 <TR> 
   <TD> 
     <A class=price href="/homes/homesforsale/view_details.jsp?advertID=14470882&listID=2492&index=1&amp"> 
       Product 2, Price 2 
     </A> 
   </TD> 
 </TR> 
 <TR> 
   <TD> 
     <A class=price href="/homes/homesforsale/view_details.jsp?advertID=14470882&listID=2492&index=1&amp"> 
       Product 3, Price 3 
     </A> 
   </TD> 
 </TR> 
 [...] 

Here is an extraction anchor as suggested by the Wizard for the first product:

EXTRACT POS=1 TYPE=TXT ATTR=<A<SP>class=price<SP>href="/homes/homesforsale/view_details.jsp?advertID=14470882&listID=2492&index=1&amp">*    

This command works fine in the Wizard, but fails during the macro execution. Why? Because the "listID" part of the URL changes every time you visit the page. You can find this out by running the Wizard twice (after refreshing the page in between) and comparing the extraction anchors. We also note that the variable "advertID" is probably the most important part of the link, since it defines the ad.

Solution: Replace the changing listID number with *:

EXTRACT POS=1 TYPE=TXT ATTR=<A<SP>class=price<SP>href="/homes/homesforsale/view_details.jsp?advertID=14470882&listID=*&index=1&amp>* 

Actually, while you are at it you can remove most static parts of the anchor as well. The result looks like:

EXTRACT POS=1 TYPE=HREF ATTR=<A<SP>class=price<SP>href="*advertID=14470882*">* 

If you want to cycle through all the ads on the page you can do this as follows:

  1. Replace the advertID number by an asterisk. Now, it will always find the matching extraction anchor.
  2. To tell iMacros go for the second (third,....) product, change the POS parameter with a variable:
EXTRACT POS={{!LOOP}} TYPE=HREF ATTR=<A<SP>class=price<SP>href="*advertID=*">*  

During runtime {{!LOOP}} takes on the values 1, 2, 3,... iMacros extracts the price on this page consecutively.

Asian Language Support

Data Extraction on Chinese Windows Version

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

Data Extraction Tip:

Western (ANSI) characters can be extracted on any language version of Windows. In order to extract Asian characters correctly, please run iMacros on a Windows system that supports the language. Example: To extract Chinese characters please run iMacros on the Chinese language version of Windows.