【问题标题】:How to write data to a file using Puppeteer?如何使用 Puppeteer 将数据写入文件?
【发布时间】:2019-11-12 00:30:59
【问题描述】:

Puppeteer 公开了一个 page.screenshot() 方法,用于在您的计算机上本地保存屏幕截图。 Here are the docs.

见:https://github.com/GoogleChrome/puppeteer
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});

有没有办法以类似的方式保存数据文件。我正在寻找类似于...的东西。

page.writeToFile({data, path,});

【问题讨论】:

  • 什么样的数据?

标签: javascript web-scraping screen-scraping chromium puppeteer


【解决方案1】:

由于任何 puppeteer 脚本都是一个普通的 node.js 脚本,你可以使用你在 node 中使用的任何东西,比如说好的旧 fs 模块:

const fs = require('fs');
fs.writeFileSync('path/to/file.json', data);

【讨论】:

  • 如果您在输出中面对 [Object Object],只需添加 JSON.stringify(data) fs.writeFileSync('path/to/file.json', JSON.stringify(data));
猜你喜欢
  • 1970-01-01
  • 2013-02-22
  • 2015-12-09
  • 2010-12-27
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2017-12-11
相关资源
最近更新 更多