最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to download file with direct link Puppeteer? - Stack Overflow

programmeradmin5浏览0评论

I need to download a image with puppeteer. the problem here, buffer return by goto method. i think it return sequence as the image load. so writeFile method only get the last buffer. Is there other promise method to tackle sequence buffer?

const puppeteer = require('puppeteer-core');
const fs = require('fs').promises;

(async () => {
  const options = {
   product: 'chrome',
   headless: true,
   pipe: true,
   executablePath: 'chrome.exe'
  };

  const browser = await puppeteer.launch(options);
  const page = await browser.newPage();
  const response = await page.goto('.png/revision/latest?cb=20161013233552');
  
  // save buffer to file
  await fs.writeFile('file.jpg', await response.buffer());
  browser.close();
})();

I need to download a image with puppeteer. the problem here, buffer return by goto method. i think it return sequence as the image load. so writeFile method only get the last buffer. Is there other promise method to tackle sequence buffer?

const puppeteer = require('puppeteer-core');
const fs = require('fs').promises;

(async () => {
  const options = {
   product: 'chrome',
   headless: true,
   pipe: true,
   executablePath: 'chrome.exe'
  };

  const browser = await puppeteer.launch(options);
  const page = await browser.newPage();
  const response = await page.goto('https://static.wikia.nocookie/naruto/images/d/dd/Naruto_Uzumaki%21%21.png/revision/latest?cb=20161013233552');
  
  // save buffer to file
  await fs.writeFile('file.jpg', await response.buffer());
  browser.close();
})();
Share Improve this question asked Mar 8, 2021 at 9:40 david stephendavid stephen 3491 gold badge5 silver badges12 bronze badges 1
  • 1 Cannot reproduce, the image is saved. Maybe you need just a proper extension like file.webp? – vsemozhebuty Commented Mar 8, 2021 at 16:40
Add a ment  | 

1 Answer 1

Reset to default 5

Download with puppeteer is an hell...

for simple downloading in a folder on you pc:

await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', 
    downloadPath: 'C:\folder...'}); // this set the destionation of file dowloaded by pup.
// then page.goto ....

This is a full-example: you can use to dowload any kind of resources, also if a login is required :-)

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

(async () => {

let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto('https://static.wikia.nocookie')

// ...eventually login

let fn = async (URI) => {
    // fetch the page from inside the html page
    const res = await fetch(URI, {'credentials': 'same-origin'});

    // attachment; filename="...."; size="1234"
    let str = res.headers.get('Content-Disposition');
    const regex = /filename="([^"]*)".*size="([^"]*)"/gm;

    let m = regex.exec(str);
    let filename = m?m[1]:null;
    let size = m?m[2]:null;
    let blob = await res.blob()
    let bufferArray = await blob.arrayBuffer();

    var base64String = btoa([].reduce.call(new Uint8Array(bufferArray),function(p,c){return p+String.fromCharCode(c)},''))

    return {base64String, size, filename};
}

let x = await page.evaluate(fn, 'https://static.wikia.nocookie/naruto/images/d/dd/Naruto_Uzumaki%21%21.png/revision/latest?cb=20161013233552')

// x.base64String <- buffer, you can write
// x.filename <- name of file downloaded
// x.size <- size

// console.log(x.blob)

await fs.writeFile('message1.png', x.base64String, {encoding: "base64"})
console.log('ok!')

})().then();
发布评论

评论列表(0)

  1. 暂无评论