python playwright 监听页面响应
时间: 2023-10-10 13:09:50 浏览: 306
监听端口响应速度的软件
你可以使用 Playwright 的 `page.on('response', handler)` 方法来监听页面的响应。
下面是一个示例代码,演示如何在 Python Playwright 中监听页面的响应:
```python
from playwright.sync_api import Playwright, sync_playwright
def main(playwright: Playwright) -> None:
browser = playwright.chromium.launch()
page = browser.new_page()
# 监听页面响应
def handle_response(response):
print(f"响应 URL: {response.url}")
print(f"响应状态码: {response.status}")
page.on("response", handle_response)
page.goto("https://example.com")
browser.close()
with sync_playwright() as playwright:
main(playwright)
```
在上面的示例中,我们使用 `page.on('response', handler)` 来注册一个监听器,当页面收到响应时,这个监听器会被调用。在监听器中,我们可以获取响应的 URL、状态码等信息。
阅读全文