异步协程多浏览器开启playwright实战

import asyncio
from playwright.async_api import async_playwright


async def run(url):
    async with async_playwright() as playwright:
        # create a chromium browser instance
        chromium = playwright.chromium
        # browser = await chromium.launch()
        #create a bowser instance which headless is true
        browser = await chromium.launch(headless=False)
        # create two isolated browser contexts
        user_context = await browser.new_context()
        page = await user_context.new_page()
        await page.goto(url)
        # await page2.goto('https://www.baidu.com')


async def main():
    tasks =[]
    urls = ['https://www.baidu.com','https://www.taobao.com','https://www.jd.com']
    for url in urls:
        task = asyncio.ensure_future(run(url))
        tasks.append(task)
    await asyncio.gather(*tasks)



if __name__ == '__main__':
    asyncio.run(main())

你可能感兴趣的:(python)