python playwright用多线程创建多标签页
时间: 2023-09-14 08:05:07 浏览: 191
多标签页面实现
使用Python Playwright创建多个标签页可以使用多线程。下面是一个示例代码:
```python
import threading
from playwright.sync_api import Playwright, sync_playwright
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://example.com')
print(page.title())
context.close()
browser.close()
def main() -> None:
with sync_playwright() as playwright:
threads = []
for i in range(5):
t = threading.Thread(target=run, args=(playwright,))
threads.append(t)
t.start()
for t in threads:
t.join()
if __name__ == '__main__':
main()
```
这个示例代码创建了5个线程,每个线程都会创建一个浏览器实例、一个上下文、一个页面,并访问了`example.com`网站。在这个示例中,我们使用了`chromium`浏览器,但你也可以使用其他支持的浏览器。
阅读全文