ramda.js 的爬虫(函数式编程)
crawler with ramda.js (functional programming)
我正在尝试从 TMDB 网站抓取电影数据。我用纯 javascript 完成了我的代码,但我想通过使用 ramda.js.
将代码更改为函数式编程风格
我在下面附上了我的代码。我想摆脱 for 循环(如果可能的话)并使用 R.pipe 函数。
(async () => {
for (let i = 0; i < 1000; i++) {
(() => {
setTimeout(async () => {
let year = startYr + Math.floor(i / 5);
await request.get(path(year, i % 5 + 1), async (err, res, data) => {
const $ = cheerio.load(data);
let list = $('.results_poster_card .poster.card .info .flex a');
_.forEach(list, (element, index) => {
listJSON.push({
MovieID: $(element).attr('id').replace('movie_', ''),
Rank: (i % 5) * 20 + index + 1,
Year: year
});
});
if(i === 1000 - 1) {
await pWriteFile(`${outputPath}/movieList.json`, JSON.stringify(listJSON, null, 2));
}
});
}, 1000 * i);
})(i);
}
})().catch(error => console.log(error));
您可以使用 Ramda range()
函数来替换您的循环。
https://ramdajs.com/docs/#range
R.range(0, 1000);
这将为您提供一组整数(您的 i
),您可以使用它们(map()
或任何您需要的)。
步骤:
1- 在小函数中分解代码
2- 停止使用 async
await
并使用 promise.then(otherFunction)
3- 使用 promise 时,您可以创建一个 sleep
函数,如下所示:const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
例如:
const process = index => sleep(1000)
.then(() => makeRequest(index))
.then(processData);
R.range(0, 1000)
.reduce(
(prev, actual) => prev.then(() => process(actual),
Promise.resolve()
) // Sequential
.then(printResult);
R.range(0, 1000)
.map(process) // Parallel
.then(printResult);
我正在尝试从 TMDB 网站抓取电影数据。我用纯 javascript 完成了我的代码,但我想通过使用 ramda.js.
将代码更改为函数式编程风格我在下面附上了我的代码。我想摆脱 for 循环(如果可能的话)并使用 R.pipe 函数。
(async () => {
for (let i = 0; i < 1000; i++) {
(() => {
setTimeout(async () => {
let year = startYr + Math.floor(i / 5);
await request.get(path(year, i % 5 + 1), async (err, res, data) => {
const $ = cheerio.load(data);
let list = $('.results_poster_card .poster.card .info .flex a');
_.forEach(list, (element, index) => {
listJSON.push({
MovieID: $(element).attr('id').replace('movie_', ''),
Rank: (i % 5) * 20 + index + 1,
Year: year
});
});
if(i === 1000 - 1) {
await pWriteFile(`${outputPath}/movieList.json`, JSON.stringify(listJSON, null, 2));
}
});
}, 1000 * i);
})(i);
}
})().catch(error => console.log(error));
您可以使用 Ramda range()
函数来替换您的循环。
https://ramdajs.com/docs/#range
R.range(0, 1000);
这将为您提供一组整数(您的 i
),您可以使用它们(map()
或任何您需要的)。
步骤:
1- 在小函数中分解代码
2- 停止使用 async
await
并使用 promise.then(otherFunction)
3- 使用 promise 时,您可以创建一个 sleep
函数,如下所示:const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
例如:
const process = index => sleep(1000)
.then(() => makeRequest(index))
.then(processData);
R.range(0, 1000)
.reduce(
(prev, actual) => prev.then(() => process(actual),
Promise.resolve()
) // Sequential
.then(printResult);
R.range(0, 1000)
.map(process) // Parallel
.then(printResult);