Apify

Apify and Crawlee Official Forum

b
F
A
J
A

Downloading an image using puppeteer example

Hi, I found this simple example using Puppeteer, that downloads images as you visit the page and I'm wonderign how I can encorporate it into my crawlee scraper.

Plain Text
    this.page.on('response', async (response) => {
      const matches = /.*\.(jpg|png|svg|gif)$/.exec(response.url());

      console.log(matches);

      if (matches && (matches.length === 2)) {
        const extension = matches[1];
        const buffer = await response.buffer();

        fs.writeFileSync(`downloads/${this.request.userData}.${extension}`, buffer, 'base64');
        counter += 1;
      }
    });
M
2 comments
Okay I figured it out:
Plain Text
      preNavigationHooks: [
        async (crawlingContext, gotoOptions) => {
          const { page, request } = crawlingContext;

          console.log('handlePageFunction');

          page.on('response', async (response) => {
            const matches = /.*\.(jpg|png|svg|gif)$/.exec(response.url());

            if (matches && (matches.length === 2)) {
              const extension = matches[1];
              const buffer = await response.buffer();

              fs.writeFileSync(
                `downloads/${request.userData.row.id}.${extension}`, 
                buffer,
                'base64'
              );
            }
          })
        }
      ]
Add a reply
Sign up and join the conversation on Discord
Join