Nested loops and scraping webpage data

I am trying to extract data from a sequence of webpages. Each page has a list of links, clicking the link takes you to the subpage with the data required. You return to the first page, select the next link and extract the next set of data. When all the links have been accessed you move to the next top level page and access a new list of links.

Logically this could be achieved with three nested “while/endwhile” loops. This is precisely how I would do this in iMacros., but nesting While loops is not supported in Kantu. (The loops break when all the links have been accessed, returning to the higher level loop.)

Has anyone found a way to do this? Workaround? Example code snippet?

Thanks.

Workaround: Use the GOTOLABEL command for the 2nd and 3rd loop.

LABEL ONE
  do something 1
  LABEL TWO
    do something 2
   While
      do something
   endWhile
 GOTOLABEL TWO
GOTOLABEL ONE

You can use “ThrowError” to stop the macro once everything is done, or you use GOTOIF and make this a conditional jump.

PS: I hope that nested loops will be supported in the future :wink:

1 Like

Perfect, thank you. I had tried “if then else” without success!

I just tested it to make sure my idea works => it does :smiley:

Example:

{
  "CreationDate": "2018-9-12",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://bbc.com",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "0",
      "Value": "l1counter"
    },
    {
      "Command": "store",
      "Target": "0",
      "Value": "l2counter"
    },
    {
      "Command": "label",
      "Target": "ONE",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "${l1counter}+1 ",
      "Value": "l1counter"
    },
    {
      "Command": "echo",
      "Target": "inside loop 1, counter = ${l1counter}",
      "Value": "blue"
    },
    {
      "Command": "label",
      "Target": "TWO",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "${l2counter}+1 ",
      "Value": "l2counter"
    },
    {
      "Command": "echo",
      "Target": "inside loop 2, counter1 = ${l1counter}, counter2 = ${l2counter}",
      "Value": "green"
    },
    {
      "Command": "gotoIf",
      "Target": "${l2counter} < 5",
      "Value": "TWO"
    },
    {
      "Command": "store",
      "Target": "0",
      "Value": "l2counter"
    },
    {
      "Command": "gotoIf",
      "Target": "${l1counter} < 5",
      "Value": "ONE"
    }
  ]
} 

3 Likes

Thank you. I suspect a lot of people will find this helpful!

Just FYI: Nested loops are supported meanwhile. See No Nested WHILE allowed?