javascript - 请求中的 node.js 多个函数

标签 javascript node.js request

我尝试像下面的代码一样从单个请求中抓取数据,但它不起作用。当我只尝试一个程序时,它起作用了。如何在一个请求过程中调用多个过程?

var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var link = "www.google.com";
request(link, function (error, response, html) {
    if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        //scrape class
        $('.someclass').filter(function () {
            var data = $(this);
            var description = data.html();
            //write data to file
            fs.appendFile('description.txt', description + "\n", function (err) {
                if (err)
                    throw err;
            });
        });
        //scrape class1
        $('.someclass1').filter(function () {
            var data = $(this);
            var description1 = data.html();
            //write data to file
            fs.appendFile('description1.txt', description1 + "\n", function (err) {
                if (err)
                    throw err;
                //console.log('The "description" was appended to file!');
            });
        });
        //scrape class2
        $('.someclass2').filter(function () {
            var data = $(this);
            var description2 = data.html();
            //write data to file
            fs.appendFile('description2.txt', description2 + "\n", function (err) {
                if (err)
                    throw err;
                //console.log('The "description" was appended to file!');
            });
        });
    }
});

最佳答案

过滤器并没有像您想象的那样工作。您正在寻找 .each()。过滤器接受一个列表并返回一个较小的列表。每个迭代项目。

function writeToFile($, methodStr, fileName, modifyFunc) {
    return function () {
        // Whoever calls this function gets its innerhtml written to whatever
        // fileName is passed to the outer function.
        var text = $(this)[methodStr]() + "\n";
        if (typeof modifyfunc === 'function') {
            text = modifyFunc(text);
        }
        fs.appendFileSync(fileName, text);
    };
}

然后像这样应用它

request(link, function (error, response, html) {
    if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        // these each statements say that for every element that has .someclass
        // give it the inner function in writeToFile where fileName is description.txt
        $('.someclass').each(writeToFile($, 'text', 'description.txt'));
        $('.someclass1').each(writeToFile($, 'html', 'description1.txt'));
        $('.someclass2').each(writeToFile($, 'text', 'description.txt2', function (str){
            return str + "Here is a change that will also get written to the file";
        }));
    }
}

关于javascript - 请求中的 node.js 多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489087/

相关文章:

c - 在 C 中解析 HTTP 请求行

javascript - 当我使用浏览器操作 Javascript 打开新标签时,Google Chrome 关闭,不知道为什么

node.js - ansible npm install 永远不会完成

node.js - 为什么 Grunt 不使用 Nodemon 启动我的 Express 服务器?

javascript - 关于函数内部的 Javascript 函数的解释?

request - Gmail API 所有邮件

javascript - 如何检查我网站上的脚本是否提出任何请求?

用于输入最大长度文本的 Javascript 验证

javascript - ApolloClient超时最佳选择

javascript - Google map 在拖动时不会更新图 block