我想在Apify中创建一个日常任务,它读取这个非常简单的csv:https://api.alternative.me/fng/?format=csv并将数据解析到Google Sheets。我是一个Apify初学者,我想知道如何用几行代码就能做到这一点。
来自柏林的最佳
发布于 2020-03-09 21:34:06
你可以在Apify平台上写一个actor,这是一个JavaScript代码。然后,您可以scheduler这段代码,让它每天/每月或任何时候运行。您甚至可以从UI手动运行它。
在javascript代码中,首先需要从URL请求CSV,为此我建议使用got和csv-parse包。之后,您将需要解析CSV并将解析后的数据导入到google sheet。您可以使用google sheets Import & Export,它已经准备好用于Apify actors的解决方案。
这里有一段简单的代码,可以帮助您开始构建参与者。
const Apify = require('apify');
const parse = require('csv-parse/lib/sync');
const got = require('got');
Apify.main(async () => {
const { body: csv } = await got('http://example.com/my.csv');
const records = parse(csv, {
columns: true,
skip_empty_lines: true
});
const updates = [];
records.forEach((record) => {
// Do something with the record
updates.push(record);
});
await Apify.call('lukaskrivka/google-sheets', {
spreadsheetId: 'your_spreadsheetId',
mode: 'append',
rawData: updates,
})
});https://stackoverflow.com/questions/60590107
复制相似问题