Scraping Cheerio - Getting only element[k]

80 views Asked by At

Problem : With this code I get a list of every iteration of a given element (h1 here)

I would like to get only the number k (k can be a variable assumption) of this element.

Ultimately, I would like to get say, the element1[i], element2[j], where element1 can be a class, id, same for element2

And then concatenate element1,2,...n in a JSON file

I think this is very basic question, but I am quite new to jQuery...

'use strict';

var url = "http://www.website.html",
rech = "h1";

// Import the dependencies
var cheerio = require("cheerio"),
req = require("tinyreq");

// Define the scrape function
function scrape(url, data, cb) {
// 1. Create the request
req(url, (err, body) => {
    if (err) { return cb(err); }

    // 2. Parse the HTML
    let $ = cheerio.load(body)
      , pageData = {}
      ;

    // 3. Extract the data
    Object.keys(data).forEach(k => {
        pageData[k] = $(data[k]).text();
    });

    // Send the data in the callback
    cb(null, pageData);
});
}

// Extract some data from my website
scrape(url, {

rech

}, (err, data) => {
console.log(err || data);
});
0

There are 0 answers