Val*_*pov 2 python selenium asynchronous webdriver python-asyncio
是否可以首先为每个任务打开浏览器,然后再打开链接?此代码引发错误
import asyncio
from selenium import webdriver
async def get_html(url):
driver = await webdriver.Chrome()
response = await driver.get(url)
Run Code Online (Sandbox Code Playgroud)
TypeError:对象WebDriver不能在“ await”表达式中使用
如果您想以异步方式使用Selenium,我建议使用Driver和一个执行程序的多个实例,如下所示:
import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from selenium import webdriver
executor = ThreadPoolExecutor(10)
def scrape(url, *, loop):
loop.run_in_executor(executor, scraper, url)
def scraper(url):
driver = webdriver.Chrome("./chromedriver")
driver.get(url)
loop = asyncio.get_event_loop()
for url in ["https://google.de"] * 2:
scrape(url, loop=loop)
loop.run_until_complete(asyncio.gather(*asyncio.all_tasks(loop)))
Run Code Online (Sandbox Code Playgroud)
请注意,您可以在无头模式下运行selenium,因此您无需生成整个GUI即可调用某些简单的url。
| 归档时间: |
|
| 查看次数: |
3987 次 |
| 最近记录: |