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

K.Rice

I use node.js and I want to get this http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 page and fetch some data I need. I wasn't able to make it with cheerio, because I've never encounter with such a kind of pages before. I'll be glad if someone tell me how to parse such pages and which node module use for it since I wasn't able to figure out it with google, however I understand that it should be easy and I'm just asking silly question.

narainsagar

Here, is simple extracted output from html output via my code.

{
   "description": "In the year 2071, humanity has colonized several of the planets and moons of the solar system leaving the now uninhabitable surface of planet Earth behind. The Inter Solar System Police attempts to 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 is code which extracts the info from page and saves it in an object (key:value) pair (i.e., like the 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 the above code you need to open http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 and paste the above script in Dev Tools inspector -> console. when you run the code it will throw result because jquery isn't found the page so manually add the jquery into your scripts via this link: https://stackoverflow.com/a/7474394/5228251

You need to use this ^code using cheerio to parse the page.

UPDATED

Use request and cheerio npm modules;

Installing modules;

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

Using this 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 the HTML for the 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 this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get data from one php page using ajax and pass it to another php page using ajax

From Dev

How to fetch data from servlet using ajax?

From Dev

How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP

From Dev

How to fetch data from mongodb and show using node.js

From Dev

how to send data from a php page to ajax page

From Dev

Pass data from one PHP page to another using ajax

From Dev

Laravel - Using Ajax to Fetch Data From Database

From Dev

how to fetch data from mysql database using php

From Dev

How to send input file data value using ajax to a php page

From Dev

How to send input file data value using ajax to a php page

From Dev

how to use a user input defined in one html page in another php page to fetch data from oracle database

From Dev

how to fetch data from mapbox to html page

From Dev

Fetch multiple data from a php file using AJAX and insert them in different input fields

From Dev

fetch value from another page using ajax in codeigniter

From Dev

PHP POST data to another page using AJAX

From Dev

How to fetch and display data while scrolling the page using php mysqli javascript

From Dev

ajax load data from php page and mysqli

From Dev

how to fetch data from sql server database in php without refreshing the page

From Dev

how to fetch data from sql server database in php without refreshing the page

From Dev

How to use data from one HTML page to retrieve data to be used on another HTML page using ajax

From Dev

How to fetch data using function in PHP?

From Dev

how to fetch data from json array in php

From Dev

Using PHP to fetch content from a DOM node but not from its children

From Dev

calling servlet from php page using ajax

From Dev

How to fetch data from json using Reactjs

From Dev

How to pass user input value from a PHP page to HTML page using AJAX

From Dev

How to pass user input value from a PHP page to HTML page using AJAX

From Dev

pass data from remote loaded content to another php page using ajax

From Dev

How to fetch data from MySQL database into nested JSON array using php script?

Related Related

  1. 1

    How to get data from one php page using ajax and pass it to another php page using ajax

  2. 2

    How to fetch data from servlet using ajax?

  3. 3

    How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP

  4. 4

    How to fetch data from mongodb and show using node.js

  5. 5

    how to send data from a php page to ajax page

  6. 6

    Pass data from one PHP page to another using ajax

  7. 7

    Laravel - Using Ajax to Fetch Data From Database

  8. 8

    how to fetch data from mysql database using php

  9. 9

    How to send input file data value using ajax to a php page

  10. 10

    How to send input file data value using ajax to a php page

  11. 11

    how to use a user input defined in one html page in another php page to fetch data from oracle database

  12. 12

    how to fetch data from mapbox to html page

  13. 13

    Fetch multiple data from a php file using AJAX and insert them in different input fields

  14. 14

    fetch value from another page using ajax in codeigniter

  15. 15

    PHP POST data to another page using AJAX

  16. 16

    How to fetch and display data while scrolling the page using php mysqli javascript

  17. 17

    ajax load data from php page and mysqli

  18. 18

    how to fetch data from sql server database in php without refreshing the page

  19. 19

    how to fetch data from sql server database in php without refreshing the page

  20. 20

    How to use data from one HTML page to retrieve data to be used on another HTML page using ajax

  21. 21

    How to fetch data using function in PHP?

  22. 22

    how to fetch data from json array in php

  23. 23

    Using PHP to fetch content from a DOM node but not from its children

  24. 24

    calling servlet from php page using ajax

  25. 25

    How to fetch data from json using Reactjs

  26. 26

    How to pass user input value from a PHP page to HTML page using AJAX

  27. 27

    How to pass user input value from a PHP page to HTML page using AJAX

  28. 28

    pass data from remote loaded content to another php page using ajax

  29. 29

    How to fetch data from MySQL database into nested JSON array using php script?

HotTag

Archive