【问题标题】:How to fetch data from ajax.inc.php page, using node modules?如何使用节点模块从 ajax.inc.php 页面获取数据?
【发布时间】:2016-09-26 15:12:45
【问题描述】:

我使用 node.js,我想获取这个 http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 页面并获取一些我需要的数据。我没能用cheerio 做到这一点,因为我以前从未遇到过这样的页面。如果有人告诉我如何解析这些页面以及使用哪个节点模块,我会很高兴,因为我无法用谷歌弄清楚它,但是我知道这应该很容易,我只是在问愚蠢的问题.

【问题讨论】:

    标签: javascript php ajax node.js


    【解决方案1】:

    这里是通过我的代码从 html 输出中简单提取的输出。

    {
       "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"
    }
    

    下面是从页面中提取信息并将其保存在对象(键:值)对中的代码(即,如上);

    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);
    

    要测试上述代码,您需要打开http://myanimelist.net/includes/ajax.inc.php?t=64&id=1 并将上述脚本粘贴到开发工具检查器 -> 控制台中。当您运行代码时,它会抛出结果,因为找不到页面,因此请通过此链接手动将 jquery 添加到您的脚本中:https://stackoverflow.com/a/7474394/5228251

    您需要使用此 ^code 和 cheerio 来解析页面。

    更新

    使用requestcheerio npm 模块;

    安装模块;

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

    使用这个脚本;

    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);
        });
    }
    

    用法:

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

    希望这会有所帮助。

    【讨论】:

    • 谢谢!它可以在浏览器中使用,但是我仍然无法理解如何在cheerio 中使用其他脚本。似乎没有机会创建元素,然后在加载的页面中使用cheerio 运行脚本。可能,我应该使用 jquery 或 phantom 模块来代替我现在使用的 request 和cheerio。而且我有很多页面要访问,所以我有点担心会花费时间来加载我的页面,其中包含每个页面的信息和库。
    • 别担心。你仍然可以和cheerio一起。基本上,cheerio 就像 node js 的 jquery。你也不需要包含 jquery。
    • 我已经更新了答案。请在 UPDATED 标题之后查找。
    • 感谢您的详细解释,但我在终端中收到了空字符串。我有 0.20.0 版本的 cheerio 和 2.72.0 version 的请求。也许我应该改变它们?
    • 取消注释 console.log(body) 并查看响应 html 检查其是否有效。
    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2022-07-10
    相关资源
    最近更新 更多