Ad

Cheerio: Preserve Line Breaks

- 1 answer

<td align="center"><font face="Verdana"><font size=2><b> 3792&nbsp;R</b><br><font size=1>tp WHR3</font><br></font><font size=1><a target="_blank" rel="nofollow noreferrer" href="http://www.lyngsat-maps.com/footprints/NSS-9-West-hemi.html">West hemi</a><br>39-40</font></td>

I want to get

  1. 3792 R
  2. tp WHR3
  3. West hemi
  4. 39-40

cheerio code -->

$('td', td).text()

get text [ 3792 Rtp WHR3West hemi39-40]

Ad

Answer

You can target specific elements inside your <td> using CSS selectors:

const value1 = $('td font[face=Verdana] font[size=2] b').text() // 3792 R
const value2 =  $('td font[face=Verdana] font[size=2] font[size=1]').text() // tp WHR3
const value3 = $('td > font[size=1] a').text() // West hemi
const value4 = $('td > font[size=1]').text().substring(value3.length - 1) // 39-40

The last value you needed to retrieve was not contained inside a specific element so I retrieved the value of the parent element which contains the value3 and value4 and, subtracted the value of value3 so what's remaining is value4

Ad
source: stackoverflow.com
Ad