1

I have a list of thousands of URLs. I want to use Python/Selenuim to:

  1. load each URL,
  2. select one element
  3. close the page

To make it run faster, I want to run lots of these processes in parallel, but I can only work out how to do it one at a time.

from selenium import webdriver
driver = webdriver.Chrome()

url_list = [
            'https://www.instagram.com/p/Bj7NmpqBuSw/?tagged=style', 
            'https://www.instagram.com/p/Bj7Nic3Au85/?tagged=style'
            ]

for url in url_list:
    driver.get(url)
    driver.find_elements_by_class_name("class-name-for-profile-link")
    driver.close()

​ I tried using lots of browser tabs

driver.switch_to.window(driver.window_handles[1])

but the handles are a bit tricky to manage.

How can I run this process in parallel?

1
  • 1
    you can also use threads, python's threading module is simple and inbuilt Commented Jun 12, 2018 at 14:00

2 Answers 2

4

tl;dr I created this gist to give an easy example of how to run simple Selenium tasks in parallel. You can adapt it to your own purposes.


The issue with parallelising Selenium scripts is that Selenium workers are themselves processes. The above script uses two FIFO queues, one that stores the IDs of idle Selenium workers and one that stores data to pass to the workers. Background master threads listen across both these queues and assign incoming data to idle workers, taking the ID of selenium workers off the worker queue while the worker does its work.

All you would need to do to adapt the code to your purposes is to change the code in the function selenium_task. Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this to loop parallel. Sample usage:

from joblib import Parallel, delayed

def do_stuff(url):
    phantom = webdriver.PhantomJS('/path/to/phantomjs') # you can use any driver
    phantom.get(url)
    # do your stuff
    phantom.close()

Parallel(n_jobs=-1)(delayed(do_stuff)(url) for url in urls) #execute parallel for all urls

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.