6

如何在 Puppeteer 中禁用图像/CSS?我看过这个教程https://www.scrapehero.com/how-to-increase-web-scraping-speed-using-puppeteer/ 但我不知道如何将它翻译成 Python

4

2 回答 2

12

基于来自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)))
于 2019-12-24T22:25:40.340 回答
0

下面的代码将按类型禁用资源:fetchimagemediafont

    page.setRequestInterception(true)

    page.on ( 'request', async request => {
        if ( request.resourceType () === 'fetch' || request.resourceType () === 'image' || request.resourceType () === 'media' || request.resourceType () === 'font' ) {
            request.abort ()
        } else {
            request.continue ()
        }
    })
于 2019-12-24T16:31:06.560 回答