javascript - How to fetch data from ajax.inc.php page, using node modules? -


i use node.js , want http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 page , fetch data need. wasn't able make cheerio, because i've never encounter such kind of pages before. i'll glad if tell me how parse such pages , node module use since wasn't able figure out google, understand should easy , i'm asking silly question.

here, simple extracted output html output via code.

{    "description": "in year 2071, humanity has colonized several of planets , moons of solar system leaving uninhabitable surface of planet earth behind. inter solar system police attempts ke...",    "genres": "action, adventure, comedy, drama, sci-fi, space",    "status": "finished airing",    "type": "tv",    "episodes": "26",    "score": "8.83",    "ranked": "#22",    "popularity": "#31",    "members": "419,197" } 

below code extracts info page , saves in object (key:value) pair (i.e., above);

var $body = $('body');  $('div').children().empty(); var description = $('div').text().trim(); var keys = $('body span').text().split(':'); keys.splice(-1, 1); $body.children().empty(); var values = $body.text().trim().split('\n');  var result = {     description: description };  for(var j = 0; j<keys.length; j++) {     result[(keys[j].tolowercase().trim())] = (values[j].trim()); }  console.log('result', result); 

to test above code need open http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 , paste above script in dev tools inspector -> console. when run code throw result because jquery isn't found page manually add jquery scripts via link: https://stackoverflow.com/a/7474394/5228251

you need use ^code using cheerio parse page.

updated

use request , cheerio npm modules;

installing modules;

$ npm install request --save $ npm install cheerio --save 

using script;

var cheerio = require('cheerio'),     request = require('request');  function scrapepage(callback) {     var result = null;     var url = 'http://myanimelist.net/includes/ajax.inc.php?t=64&id=1';      request(url, function (error, response, body) {         if (!error && response.statuscode == 200) {             // console.log(body) // show html page url.              var $ = cheerio.load('<body>' + body + '</body>');             var $body = $('body');              $('body div').children().empty();             var description = $('body div').text().trim();             var keys = $('body span').text().split(':');             keys.splice(-1, 1);             $body.children().empty();             var values = $body.text().trim().split('\n');              result = {                 description: description             };              for(var j = 0; j<keys.length; j++) {                 result[(keys[j].tolowercase().trim())] = (values[j].trim());             }         }          callback(result);     }); } 

usage:

scrapepage(function(result) {     console.log('result', result); }); 

hope helps.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -