i am trying to scrape a website and i don't know how to get the selectors i want. I am using this function:
function scrapeData(urls) {
var data = [];
var urlPromises = urls.map(function(url) {
return request(url).spread(function(response, html){
if (response.statusCode == 200) {
var $ = cheerio.load(html);
var elements = $('.IframePrincipal').toArray();
// Items to scrape
return elements.map(function(el, index){
var title = $(el).find('h2').text().replace(/^\s+|\s+$/g, "");
var stock = $('#FormASP > table:nth-child(4) > tbody > tr > td > table > tbody > tr:nth-child(2) > td.IframePrincipal > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(3) > td > table > tbody > tr > td').html();
var price = parseFloat($(el).find('td:nth-child(1)').text().replace(/\€|,/g, '.'));
// Push items into data array
return {Title: title, Stock: stock, Price: price, date: date};
});
}
else {
return [];
}
}, function(error){
console.log("Error");
return [];
});
});
return Promise.all(urlPromises).then(flatten);
}
Var title works like a charm. Var Stock doesn't, its just the css path i copypasted, and var price its from a previous website, anyway i dont know hot to get it either. I am just getting "null".
This is an example of url to scrape : Site to scrape
Ok, i was looking inspectors path instead of real code one.