Get text of a tag in different format in cheerio

I’m using cheerio npm package to extract the datas from the websites for my node app

Below is the snippet of the code from my app:

const response = await axios.get(   "https://cors-anywhere.herokuapp.com/https://example.com/search/?q=" +   param ); console.log("Response got"); const $ = cheerio.load(response.data);  const chunk1 = $("h1.text-primary a ").text(); console.log(chunk1);

With the above code I’m targetting the text from the following tag:

<h1 class="text-primary font-weight-bold media-heading h4" itemprop="title">   <a href="/teacher-107/" class="no-uline" title="Teacher"> Teacher </a>   <meta itemprop="employmentType" content="Full Time"> </h1>

The issue I’m having is there are 4 tag block like this, cheerio does gets the text of the tag but gives text from all 4 tag as a single string like: Teacher Plumber Police Army but would like to have it in array, object or separate string.

How do I do that ??

Add Comment
1 Answer(s)

Ok, I figured it out myself

 let jobTitles=[]; $('h1.text-primary ').find('a').each(function(index, element) {   jobTitles.push($(element).text());   index });

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.