我一直在运行以下代码,以便csv从网站下载文件http://niftyindices.com/resources/holiday-calendar:
csv
http://niftyindices.com/resources/holiday-calendar
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: true}); const page = await browser.newPage(); await page.goto('http://niftyindices.com/resources/holiday-calendar'); await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '/tmp'}) await page.click('#exportholidaycalender'); await page.waitFor(5000); await browser.close(); })();
有了headless: false它,它将文件下载到中/Users/user/Downloads。与headless: true它不起作用。
headless: false
/Users/user/Downloads
headless: true
我正在使用puppeteer版本在macOS Sierra(MacBook Pro)上运行此程序,该版本1.1.1将Chromium版本拉66.0.3347.0入.local-chromium/目录并使用npm init并npm i --save puppeteer进行设置。
1.1.1
66.0.3347.0
.local-chromium/
npm init
npm i --save puppeteer
知道怎么了吗?
预先感谢您的时间和帮助,
此页面通过创建逗号分隔的字符串并通过设置数据类型来强制浏览器下载csv,从而下载csv
let uri = "data:text/csv;charset=utf-8," + encodeURIComponent(content); window.open(uri, "Some CSV");
chrome上的此按钮会打开一个新标签。
您可以点击此事件,然后将内容实际下载到文件中。不知道这是否是最好的方法,但是效果很好。
const browser = await puppeteer.launch({ headless: true }); browser.on('targetcreated', async (target) => { let s = target.url(); //the test opens an about:blank to start - ignore this if (s == 'about:blank') { return; } //unencode the characters after removing the content type s = s.replace("data:text/csv;charset=utf-8,", ""); //clean up string by unencoding the %xx ... fs.writeFile("/tmp/download.csv", s, function(err) { if(err) { console.log(err); return; } console.log("The file was saved!"); }); }); const page = await browser.newPage(); .. open link ... .. click on download link ..