Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  95] [ 6]  / answers: 1 / hits: 15237  / 10 Years ago, sun, april 27, 2014, 12:00:00

I'm trying to get the title tag of a url with cheerio. But, I'm getting empty string values. This is my code:



app.get('/scrape', function(req, res){

url = 'http://nrabinowitz.github.io/pjscrape/';

request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);

var title, release, rating;
var json = { title : , release : , rating : };

$('title').filter(function(){
//var data = $(this);
var data = $(this);
title = data.children().first().text();
release = data.children().last().children().text();

json.title = title;
json.release = release;
})

$('.star-box-giga-star').filter(function(){
var data = $(this);
rating = data.text();

json.rating = rating;
})
}


fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){

console.log('File successfully written! - Check your project directory for the output.json file');

})

// Finally, we'll just send out a message to the browser reminding you that this app does not have a UI.
res.send('Check your console!')
})
});

More From » node.js

 Answers
48
request(url, function (error, response, body) 
{
if (!error && response.statusCode == 200)
{
var $ = cheerio.load(body);
var title = $(title).text();
}
})


Using Javascript we extract the text contained within the title tags.


[#71280] Thursday, April 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oliverkarle

Total Points: 96
Total Questions: 71
Total Answers: 105

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;