playwright 不加载图片
时间: 2023-11-26 12:03:14 浏览: 128
创作不易,不需要上传图片
以下是使用 Playwright 不加载图片的示例代码:
```python
from playwright.sync_api import Playwright, sync_playwright
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=True)
context = browser.new_context(
bypass_csp=True,
ignore_https_errors=True,
permissions=["geolocation"],
viewport={"width": 1920, "height": 1080},
record_video_dir="videos/",
record_video_size={"width": 1920, "height": 1080},
record_video_frame_rate=60,
)
# 在新的上下文中设置请求拦截器,禁止加载图片
def intercept_request(route, request):
if request.resource_type == "image":
route.abort()
else:
route.continue_()
context.route("**/*", intercept_request)
page = context.new_page()
page.goto("https://example.com")
# 在这里进行其他操作
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
```
在上述代码中,我们使用了 Playwright 的请求拦截器功能,拦截了所有的图片请求并将其中止,从而达到了不加载图片的目的。
阅读全文