Home > database >  how to change null value if tag not available using regex app script to scrap html
how to change null value if tag not available using regex app script to scrap html

Time:04-18

how to change null value if tag not available using regex app script to scrap html.

the code has multi class and multi td tag:

the HTML:

<tr dir="row">
  <td class='' data='#' >
                                    -
                                </td>
  <td class='' data='#' >
                                    -
                                </td>

</tr>

<tr dir="row">

  <a class='link'  href='http://www.facebook.com' target='_blank'>
  </a>

  <td class='' data='#' >
                                    -
                                </td>

</tr>

my code regex:

rows = ****************
url= *******************

for(var i = 0; i < 2; i  ){

    //var name = rows[i].match(table)
 }

if use this code is return:

1:50 PM Notice  Execution started
9:41:51 PM  Info    null
9:41:51 PM  Info    [www.facebook.com]
9:41:51 PM  Notice  Execution completed

I want to get just facebook value I use:

for(var i = 0; i < 2; i  ){

        //var name = rows[i].match(table)[0] // to first value OR [1] to get second value
     }

I get the error:

10:08:02 PM Notice  Execution started
10:08:04 PM Error   
TypeError: Cannot read property '0' of null

because the tag <a class= can't find in first but find in second: how can I use it without this error

CodePudding user response:

Change this:

var name = rows[i].match(table)[0]

to:

var match = rows[i].match(table) 
var name = match ? match[0] : ""

Reason: match(table) is returning null sometimes

  • Related