如何在aws python

时间:2018-03-29 12:56:23

标签: python-3.x amazon-web-services selenium web-scraping selenium-chromedriver

我正在使用selenium来抓取,而下面的代码在本地路径上工作。我收到WebDriverException:消息:未知错误:找不到Chrome二进制文件。我不确定我是否收到此错误,因为我的路径错误或者我必须安装一些东西?我在google上花了好几个小时试图解决这个问题,但没有运气..请帮助!!

chrome_path = "/home/ec2-user/chrome/chromedriver"
driver = webdriver.Chrome(chrome_path)

book_categories_list = []
for i in asin:
    try:
        url = u"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords="+str(i)
        driver.get(url)
        #time.sleep(1)
        books = driver.find_element_by_class_name('a-expander-container') 
        book_categories_list.append(books.text)
    except:
        book_categories_list.append('Unknown') 

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-6-436f3e3e0dac> in <module>()
      1 # Set driver and chrome driver path
      2 chrome_path = "/home/ec2-user/chrome/chromedriver"
----> 3 driver = webdriver.Chrome(chrome_path)
      4 
      5 # Url that I will access to scrape

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options)
     73                 command_executor=ChromeRemoteConnection(
     74                     remote_server_addr=self.service.service_url),
---> 75                 desired_capabilities=desired_capabilities)
     76         except Exception:
     77             self.quit()

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    152             warnings.warn("Please use FirefoxOptions to set browser profile",
    153                           DeprecationWarning)
--> 154         self.start_session(desired_capabilities, browser_profile)
    155         self._switch_to = SwitchTo(self)
    156         self._mobile = Mobile(self)

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    241         parameters = {"capabilities": w3c_caps,
    242                       "desiredCapabilities": capabilities}
--> 243         response = self.execute(Command.NEW_SESSION, parameters)
    244         if 'sessionId' not in response:
    245             response = response['value']

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    310         response = self.command_executor.execute(driver_command, params)
    311         if response:
--> 312             self.error_handler.check_response(response)
    313             response['value'] = self._unwrap_value(
    314                 response.get('value', None))

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.9.81-35.56.amzn1.x86_64 x86_64)

1 个答案:

答案 0 :(得分:0)

错误确实给我们提示如下:

WebDriverException: Message: unknown error: cannot find Chrome binary

该错误实质上意味着 ChromeDriver 未在预期位置找到 Chrome 二进制文件。

可以有以下两种解决方案:

  • 根据Requirements Chrome 安装需要位于明确的位置。

  • 作为替代方案,您可以通过Options类的实例传递 Chrome 二进制文件的绝对路径,如下所示:

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    
  • 最后,在启动 WebDriver WebClient 时,您需要发送参数 Key executable_path 使用 chrome_path

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    chrome_path = "/home/ec2-user/chrome/chromedriver"
    driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=options)