我不太熟悉高级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连接到db。建议的方法是什么?
这是我的表:
+----+-----------------------------------------------------+---------+
| 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 }));
然后,在任务函数结束时,更新表行。
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=$1 WHERE id=$2', [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=$1 WHERE id=$2', [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();
})();