如何使用剧作家Python异步打开多个页面?
我想使用Playwright for Python一次打开多个URL。但我正在努力想办法。这来自异步文档:如何使用剧作家Python异步打开多个页面?,python,web-scraping,webautomation,playwright,playwright-python,Python,Web Scraping,Webautomation,Playwright,Playwright Python,我想使用Playwright for Python一次打开多个URL。但我正在努力想办法。这来自异步文档: async def main(): async with async_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = await browser_type.launch() page = await
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto("https://scrapingant.com/")
await page.screenshot(path=f"scrapingant-{browser_type.name}.png")
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
这将按顺序打开每个浏览器类型。如果我想同时做这件事,我该怎么做呢?如果我想用URL列表做类似的事情,我该怎么做呢
我试着这样做:
urls = [
"https://scrapethissite.com/pages/ajax-javascript/#2015",
"https://scrapethissite.com/pages/ajax-javascript/#2014",
]
async def main(url):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
page = await browser.newPage()
await page.goto(url)
await browser.close()
async def go_to_url():
tasks = [main(url) for url in urls]
await asyncio.wait(tasks)
go_to_url()
但这给了我以下错误:
92: RuntimeWarning: coroutine 'go_to_url' was never awaited
go_to_url()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我相信您需要使用相同的方法调用您的
go\u to\u url
函数:
asyncio.get\u event\u loop()。运行\u直到完成(转到\u url())