Skip to content

Instantly share code, notes, and snippets.

@rootasjey
Last active April 22, 2016 04:44
Show Gist options
  • Save rootasjey/ce68cb4cc0098a8a1f7b649bb8d833d1 to your computer and use it in GitHub Desktop.
Save rootasjey/ce68cb4cc0098a8a1f7b649bb8d833d1 to your computer and use it in GitHub Desktop.
jsdom html parsing from server-side
// Get the last articles from the blog
.get('/getblog', function (req, res) {
var jqueryPath = __dirname + "/public/js/jquery-2.1.1.min.js";
jsdom.env(
"http://rootasjey.github.io/",
[jqueryPath],
function (errors, window) {
// Jquery object
var $ = window.$;
// Posts titles array
var postTitles = $(".post .post-title a");
// Posts summaries array
var postSummaries = $(".post .post-excerpt p");
// Post links array
var postLinks = $(".post .post-excerpt p .read-more");
// json array to return
var jsonArray = [];
for (var i = 0; i < postTitles.length; i++) {
var title, summary, link;
title = $(postTitles[i]).text();
summary = $(postSummaries[i]).text();
link = $(postLinks[i]).attr("href");
jsonArray.push({"title" : title, "summary" : summary, "link" : link});
}
res.send(200, jsonArray);
}
);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment