python代码示例 playwright异步批量打开网页
时间: 2023-06-01 15:06:55 浏览: 199
playwright-1.44.0
以下是一个使用playwright异步批量打开网页的Python代码示例:
``` python
import asyncio
from playwright.async_api import Playwright, async_playwright
async def open_page(browser_type: str, url: str):
async with async_playwright() as p:
browser: Playwright = await getattr(p, browser_type).launch()
page = await browser.new_page()
await page.goto(url)
await asyncio.sleep(3)
content = await page.content()
print(content)
await browser.close()
async def main():
urls = [
"https://www.google.com",
"https://www.bing.com",
"https://www.baidu.com",
"https://www.yahoo.com",
]
browser_types = ["chromium", "firefox", "webkit"]
tasks = []
for browser_type in browser_types:
for url in urls:
tasks.append(open_page(browser_type, url))
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
```
请注意,此代码示例仅供参考,实际应用中需要根据具体情况进行修改。
阅读全文