Tips for extracting text from nested elements

I have a collection of available job listings stored in my temporary variable. I am interested in extracting specific text from these listings individually. How can I retrieve text from nested classes?

In the provided code snippet, I encountered empty lines being printed to my console when attempting to access the text.

const request = require('request')
const cheerio = require('cheerio')

url = 'https://www.temp.com/jobs'

request(url, (error, response, html) => {
    if (!error && response.statusCode == 200){
        const $ = cheerio.load(html);
        const temp = $('.col-md-12.col-lg-12.col-xs-12.padding-none.job-container.jobs-on-hover')
        temp.each(function (i, e) {
            console.log($(this).children('.latest-jobs-title.font-16.margin-none.inline-block').text());        // encountering empty lines
        });
    }
})

HTML Inspection Code There are multiple elements similar to the class structure described below which I am trying to fetch in the aforementioned code.

<div class="col-md-12 col-lg-12 col-xs-12 padding-none job-container jobs-on-hover">
... (nested HTML structure)

I aim to extract the text for the ago-text class element.

Answer №1

The reason you should use the method .find() instead of .children() is because the former goes through all levels of children in the DOM tree while the latter only goes down one level. It's sufficient to specify .job-container and .latest-jobs-title without including unnecessary Bootstrap styles in your query selector.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Maintain a fixed element and enable the rest of the elements to scroll as the mobile browser address bar collapses while scrolling upwards

Currently facing a challenge where I want the background image to remain static while the address bar and content underneath scroll up. The image occupies 90% of the view height, and although I've prevented it from resizing and jumping when the addres ...

Maintaining the format of forms input in Rails and HTML: Tips and tricks

Hey there! I'm working on a Rails app that has a simple HTML form. One of the fields is called description, which is a text_area. I want to be able to display its input exactly as it was entered on the index page. For instance: If someone enters a l ...

Sinon - observing the constructor function

While I've come across a few related inquiries, none seem to address what I am specifically looking to achieve. My goal is to monitor a constructor method in such a way that when an object created with the constructor calls this method from a differe ...

Table displaying data with a filter feature that excludes specific items based on their class

I have multiple HTML tables on my website, so I created a class called datatable to meet my generic table needs. For exports like PDF and Excel, I successfully excluded columns with the ignore class. Now, I need to dynamically exclude columns with the ign ...

Detecting answering machines with Twilio

Hello, I work as a firmware developer at a startup based in Paris. Currently, we are incorporating Twilio into our product and facing an issue with stopping calls to voice machines using the IfMachine parameter. Despite our efforts, we have not been succes ...

Enhancing a prototype instance in TypeScript while activating strict mode

When working with an instance named remote from a factory in a vendor script, I encountered the need to add my own methods and members to that instance. While seeking a solution, I came across an insightful response on extending this in a Typescript class ...

Issue: Database not updating after input via AJAXExplanation: Despite submitting new data through AJAX, the

I've been struggling to display the UPDATED database on my initial html page after submitting new information. Despite successfully updating the database using AJAX, I can only see the outdated version. Any assistance in resolving this issue would be ...

Ways to ensure the image stays fixed even as the screen dimensions fluctuate. HTML/CSS

Struggling to create an HTML header with a title and image positioned on the right side, but facing issues with the image shifting over the title when screen size changes. How can I ensure the image stays in place? Have tried various solutions without succ ...

Fancybox 2 - CSS styles vanish when using Ajax request

I have a fancybox2 with static dummy content that has styling applied. It works fine, but now I need to load dynamic content via ajax. However, when I make the ajax call, the required content loads but loses all css styling, most likely due to changes in t ...

The continuation of unraveling the mystery of utilizing `overflow-y:scroll` in a horizontal slider

As a beginner, I realized too late that adding code in comments is not an option. Consequently, I decided to create a new question and ask for your assistance once again. To optimize the use of space, I have implemented bxSlider with fixed dimensions (wid ...

Error message: The function .datepicker('getDate') caused an Uncaught TypeError: Data[option] error

Encountering an issue with the code data[option] is not a function when attempting to utilize .datepicker('getDate'). The datepicker was initialized using this code: <h4>Transaction Date:</h4> From: <input id="sta ...

Field must have a base type specified to proceed

Currently, I am in the process of developing a discord.js bot. The structure I have set up involves a folder that contains all the commands, and when a command is called, index.js directs it to the appropriate file. However, when attempting to run the bot, ...

In Node JS, the variable ID is unable to be accessed outside of the Mongoose

When working with a Mongoose query, I encountered an error where I am trying to assign two different values to the same variable based on the query result. However, I keep getting this error: events.js:187 throw er; // Unhandled 'error' ev ...

Failed to install angular-cli globally on the system

When attempting to globally install angular-cli, I encountered some errors. What steps should I take? C:\Users\Jahidul>npm install -g angular-cli npm WARN registry Using stale data from http://registry.npmjs.org/ because the host is inaccess ...

What is the process of using an if statement in jQuery to verify the existence of a property in a JSON file?

I am working on a task to incorporate an if statement that checks for the existence of a specific property in a JSON file. If the property exists, I need to display its value within HTML tags <div class='titleHolder'> and "<div class=&ap ...

Examining a V-If using Jest for unit testing

How can I effectively test the v-if condition in my parent component using Jest? Parent Component: <div class="systemIsUp" v-if="systemStatus == true"> foo </div> <div class="systemIsDown" v-e ...

Creating an offline private registry with verdaccio can be achieved by following these steps

One of my recent projects involved setting up a private npm registry using verdaccio. I needed to ensure that running npm install --registry="http://localhost:4873" would fetch all dependencies from the private registry. To achieve this, I had to publish ...

Preventing the cascading effects of past hovers with AngularJS

I am working with AngularJS and have the following HTML code snippet. <div class="row" style="margin-left:60px; text-align:center;"> <div class="col-xs-1 " style="margin-left:25px;width:10px; " ng-repeat="image_thumb_id in image_thumbnail_ ...

Creating a table in VueJs and populating it with values retrieved from an MSSQL database in a .NET Core web application

I am developing a table within a .NET Core Web Application that includes multiple rows and columns filled with data retrieved from a MSSQL server through a WEB API Given the need for numerous rows and columns, I am considering using a for loop inside my & ...

What is the best way to destructure an array enclosed within the Promise keyword in JavaScript?

Currently, I am attempting to extract information from a PSQL table using the following code: async function requestData() { var selectQuery = `SELECT "fName", "lName", "phoneNumber", "eMail" FROM public."Use ...