前言
对于爬虫的小伙伴来说,有时候只需抓取页面的文本,不用加载图片,可以加快操作页面速度,那么我们可以设置禁止加载图片等资源。
禁止图片加载
根据url地址的后缀,图片资源后缀一般是png,jpg,jpeg,gif等格式。
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
# 方法1 根据url地址后缀
page.route("**/*.{png,jpg,jpeg,gif}", lambda route: route.abort())
page.goto('https://www.cnblogs.com/yoyoketang')
page.pause()
运行效果如下,会看到图片没加载
根据资源类型禁用
资源类型一般有 “document”, “stylesheet”, “script”, “image”, “font”, “xhr” 等。
from playwright.sync_api import sync_playwright