javascriptlistweb-scrapingcheerio

How do I crawl the span value only where < strong > is 'Latitude'? NodeJS Cheerio


Suppose this HTML list:

<li>
<strong> Admin name </strong>
<span itemprop="addresslocality"> Amsterdam </span>
<li>
 <li>
<strong> Latitude </strong>
<span itemprop="addresslocality">  52.370216 </span>
<li>
 <li>
<strong> Longitude </strong>
<span itemprop="addresslocality">  4.895168 </span>
<li>

How do I get the span value of Latitude and Longitude? If I just .get() the second and third itemprop it gets me unwanted values sometimes. (based on number of itemprop attributes in a list)


Solution

  • Try using contains selector:

    dom.find('strong:contains("Latitude")').next().text().trim() // 52.370216