Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load the http module to create an http server.
- var http = require('http');
- // Configure our HTTP server to respond with Hello World to all requests.
- var server = http.createServer(function (request, res) {
- res.writeHead(200, "OK", {
- 'Content-Type': 'text/html'
- });
- var request = require('request');
- var cheerio = require('cheerio');
- request('https://news.ycombinator.com', function (error, response, html) {
- if (!error && response.statusCode == 200) {
- var $ = cheerio.load(html);
- var parsedResults = [];
- $('span.comhead').each(function (i, element) {
- // Select the previous element
- var a = $(this).prev();
- // Get the rank by parsing the element two levels above the "a" element
- var rank = a.parent().parent().text();
- // Parse the link title
- var title = a.text();
- // Parse the href attribute from the "a" element
- var url = a.attr('href');
- // Get the subtext children from the next row in the HTML table.
- var subtext = a.parent().parent().next().children('.subtext').children();
- // Extract the relevant data from the children
- var points = $(subtext).eq(0).text();
- var username = $(subtext).eq(1).text();
- var comments = $(subtext).eq(2).text();
- // Our parsed meta data object
- var metadata = {
- rank: parseInt(rank),
- title: title,
- url: url,
- points: parseInt(points),
- username: username,
- comments: parseInt(comments)
- };
- // Push meta-data into parsedResults array
- parsedResults.push(metadata);
- });
- // Log our finished parse results in the terminal
- console.log(parsedResults);
- res.write("<div>" + parsedResults + "</div>");
- }
- });
- res.write("Hola que tal");
- res.end();
- });
- // Listen on port 8000, IP defaults to 127.0.0.1
- server.listen(8000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement