Snippets: Python: Puppeteer: Download Image by Screenshot

20th August 2020 at 2:19pm
Snippets: Python: Puppeteer
import asyncio

from pyppeteer import launch


async def main():
    browser = await launch({'headless': True, 'args': ['--no-sandbox']})
    page = await browser.newPage()
    await page.setViewport({"width": 1280, "height": 926})
    await page.goto('https://intoli.com/blog/saving-images/')
    await page.waitForSelector('#svg')

    # Select the #svg img element and save the screenshot.
    svg_image = await page.querySelector('#svg')
    await svg_image.screenshot({
        'path': 'logo-screenshot.png',
        'omitBackground': True,
    })

    await browser.close()


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())