Reputation: 157
I am trying to learn about webscraping and how to use cheerio.js for scraping text from DOM elements. Now, here is my problem. I have a div tag inside which I have another div tag and then inside the second div tag, I have a a
tag and I want to extract the text from the
tags. How do I go about it ?
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
//All the web scraping magic will happen here
url = 'https://ihub.co.ke/jobs';
// The structure of our request call
// The first parameter is our URL
// The callback function takes 3 parameters, an error, response status code and the html
request(url, function(error, response, html){
// First we'll check to make sure no errors occurred when making the request
if(!error){
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
console.log('Html data',$);
// Finally, we'll define the variables we're going to capture
var title;
var json = { title : ""};
thing = $(".container-fluid-post-job-link").text();
console.log('Thing',thing);
}
})
})
app.listen('8081')
console.log('Magic happens on port 8081');
HTML
<div class="container-fluid jobs-board">
<div class="container-fluid post-job-link">
<p>Advertise a Job Vacancy for KES 1,500 for 2 months.</p>
<p><a href="/myjobs" class="btn btn-outline-primary btn-md btn-block">Post Job</a></p>
</div>
//I want to extract text from the 2 <p> tags that are inside the <div> tag which has a class = container-fluid post-job-link
Upvotes: 0
Views: 1165
Reputation: 5069
You need to use correct query selector, use .container-fluid.post-job-link
instead of .container-fluid-post-job-link
, following example may help you
thing = $(".container-fluid.post-job-link").text();
or particular text
thing = $(".container-fluid.post-job-link").find('a').first().text();
Upvotes: 1