python-3.xpuppeteerpyppeteer

How to disable Images/CSS in Puppeteer?


How to disable images/CSS in Puppeteer? I've seen this tutorial https://www.scrapehero.com/how-to-increase-web-scraping-speed-using-puppeteer/ but I don't know how to translate it to Python


Solution

  • Based on example from https://github.com/miyakogi/pyppeteer/blob/dev/pyppeteer/page.py#L312:

    await page.setRequestInterception(True)
    async def intercept(request):
        if any(request.resourceType == _ for _ in ('stylesheet', 'image', 'font')):
            await request.abort()
        else:
            await request.continue_()
    page.on('request', lambda req: asyncio.ensure_future(intercept(req)))