gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:51:17 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com