【发布时间】:2018-11-06 06:00:53
【问题描述】:
我正在使用 Puppeteer 以使用 async/await 方法抓取网页。
为了抓取数据,我首先要使用网页浏览器搜索 600 多个值,因此我创建了一个包含这些值的 CSV 文件。
我导入 csv-parser 来读取文件,然后在 csv 中的每一行中声明一个字符串,以便以后可以在浏览器中搜索每个字符串的内容。
这是我整理的代码:
const csv = require('csv-parser');
(async function main (){
try{
fs.createReadStream('values.csv')
.pipe(csv())
.on('data', async function (data) {
const str = String(data.Row1)
// … Here’s the code to open headless Chrome and open the webpage (working fine)
// Code to type in the string into the search bar and click the search button:
await page.type('#SearchBar', str);
await page.click('#SearchButton');
// … Here’s the code to scrape the data displayed after each search (working fine)
});
})();
当我运行代码时,它试图打开 600 多个无头浏览器并出现以下消息:
(node:9568) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个 SIGHUP 侦听器。使用emitter.setMaxListeners() 增加限制
然后我尝试读取一个只有 10 行的 CSV 文件,它工作正常。但是我需要将所有 600 个值包含到文件中,因此我尝试通过使用 async 模块使函数将回调作为其第二个参数来将搜索次数限制为 10,但它不起作用。
¿如何一次异步搜索 10 个值,即将每次迭代的读取限制为 10 行?
如果我没有足够清楚地提出我的问题,或者如果有更简单的方法可以实现相同的目标,我深表歉意,但我在编码方面的经验为零,我只是在尝试为我父母的办公室开发一个工具我明年离开。
【问题讨论】:
标签: node.js csv web-scraping puppeteer