Chrome crashes after several hours while multiprocessing using Selenium through Python

前端 未结 2 2112
感情败类
感情败类 2020-12-04 03:59

This is the error traceback after several hours of scraping:

The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeD         


        
2条回答
  •  我在风中等你
    2020-12-04 04:15

    Right now im using this threading module to instantiate one Webdriver each thread

    import threading
    threadLocal = threading.local()
    
    def get_driver():
        browser = getattr(threadLocal, 'browser', None)
        if browser is None:
            chrome_options = Options()
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument("--headless")
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument("--lang=en")
            chrome_options.add_argument("--start-maximized")
            chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
            chrome_options.add_experimental_option('useAutomationExtension', False)
            chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36")
            chrome_options.binary_location = "/usr/bin/google-chrome"
            browser = webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver', options=chrome_options)
            setattr(threadLocal, 'browser', browser)
        return browser
    

    and it really helps me to scrape faster than executing one driver at a time.

提交回复
热议问题