简体   繁体   中英

how to open different urls at the same time by using python selenium?

is this possible to open 50 different urls at the same time using selenium with python? is this possible using threading?

If so, how would I go about doing this? If not, then what would be a good method to do so?

well, open 50 urls at the same time seems unreasonable and will require a LOT of processing, but, is possible. However i would recommend you to use a form of iteration opening one url at a time. 50 times.

list = ['list of urls here','2nd url'...]
driver = webdriver.Firefox()
for i in list:
  moving = driver.get(i)
  ...#rest of your code
driver.quit()

but... you can make one driver.get('url') for each url you want... using different drivers. Or tabs. But it will require a lot of processing.

You can try below to open 50 URLs one by one in new tab :

urls = ["http://first.com", "http://second.com", ...]

for url in urls:
    driver.execute_script('window.open("%s")' % url)

you can use celery (Distributed Task Queue) to open all these urls.

or you can use async and await with aiohttp on python >= 3.5 , which runs a single thread on a single process but concurrently(utilises the wait time on urls for fetching other urls) here is the code sample for the same. Loop takes care of scheduling these concurrent tasks.

#!/usr/local/bin/python3.5
import asyncio
from aiohttp import ClientSession

async def hello(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
           response = await response.read()
           print(response)

loop = asyncio.get_event_loop()

loop.run_until_complete(hello("http://httpbin.org/headers"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM