python playwright 监听懒加载页面响应
时间: 2023-09-22 21:07:42 浏览: 335
如果你需要监听懒加载页面的响应,你可以使用 Playwright 的 `page.waitForEvent('response')` 方法来等待页面响应。
下面是一个示例代码,演示如何在 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.wait_for_selector("#lazy-loaded-element")
# 等待页面响应
response = page.wait_for_event("response")
# 处理页面响应
handle_response(response)
browser.close()
with sync_playwright() as playwright:
main(playwright)
```
在上面的示例中,我们首先使用 `page.wait_for_selector(selector)` 方法等待页面中的某个元素出现,以触发懒加载。然后,我们使用 `page.wait_for_event(event)` 方法等待页面收到响应。最后,我们在监听器中处理响应。
阅读全文