如何从 PostgreSQL 中提取数据,处理,然后存储在 javascript 中?

How to pull data from PostgreSQL, process, then store in javascript?

我对高级 javascript 不太熟悉,正在寻找一些指导。我希望使用 puppeteer-cluster 将网页内容存储到数据库中 这是一个开始的例子:

const { Cluster } = require('puppeteer-cluster');

(async () => {
  const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_CONTEXT,
    maxConcurrency: 2,
  });

  await cluster.task(async ({ page, data: url }) => {
    await page.goto(url);
    const screen = await page.content();
    // Store content, do something else
  });

  cluster.queue('http://www.google.com/');
  cluster.queue('http://www.wikipedia.org/');
  // many more pages

  await cluster.idle();
  await cluster.close();
})();

看来我可能必须使用 pg addon 来连接数据库。推荐的方法是什么?

这是我的 table:

+----+-----------------------------------------------------+---------+
| id | url                                                 | content |
+----+-----------------------------------------------------+---------+
| 1  | https://www.npmjs.com/package/pg                    |         |
+----+-----------------------------------------------------+---------+
| 2  | https://github.com/thomasdondorf/puppeteer-cluster/ |         |
+----+-----------------------------------------------------+---------+

我想我必须将数据拉入一个数组 (id & url),并且在每次收到内容后,将其存储到数据库中(按 id & 内容)。

您应该在任务函数之外创建数据库连接:

const { Client } = require('pg');
const client = new Client(/* ... */);
await client.connect();

然后查询数据并将其排队(使用 ID 以便稍后能够将其保存在数据库中):

const rows = await pool.query('SELECT id, url FROM your_table WHERE ...');
rows.forEach(row => cluster.queue({ id: row.id, url: row.url }));

然后,在任务函数的末尾,更新 table 行。

await cluster.task(async ({ page, data: { id, url, id } }) => {
    // ... run puppeteer and save results in content variable
    await pool.query('UPDATE your_table SET content= WHERE id=', [content, id]);
});

总的来说,您的代码应该如下所示(请注意,我自己没有测试过代码):

const { Cluster } = require('puppeteer-cluster');
const { Client } = require('pg');

(async () => {
    const client = new Client(/* ... */);
    await client.connect();

    const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_CONTEXT,
        maxConcurrency: 2,
    });

    await cluster.task(async ({ page, data: { id, url } }) => {
        await page.goto(url);
        const content = await page.content();
        await pool.query('UPDATE your_table SET content= WHERE id=', [content, id]);
    });

    const rows = await pool.query('SELECT id, url FROM your_table');
    rows.forEach(row => cluster.queue({ id: row.id, url: row.url }));

    await cluster.idle();
    await cluster.close();
})();