【发布时间】:2020-08-30 10:23:24
【问题描述】:
我正在使用 scrapy selenium https://pypi.org/project/scrapy-selenium/ 来驱动我的蜘蛛,并将它托管在 Heroku 上。 我的挑战是我似乎无法解决如何配置路径以找到 chromedriver 以及 heroku 运行应用程序所需的 chrome 二进制文件
我已经设置好环境并安装了 buildpacks
BuildPacks
$ heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-chromedriver
$ heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-google-chrome
在我的 heroku 配置变量中设置环境
GOOGLE_CHROME_PATH = '/app/.apt/usr/bin/google_chrome'
CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
现在我迷路的地方是如何配置我的上述设置以在我的蜘蛛的 settings.py 文件中使用此选项
SELENIUM_DRIVER_NAME = 'firefox'
SELENIUM_DRIVER_EXECUTABLE_PATH = which('geckodriver')
SELENIUM_DRIVER_ARGUMENTS=['-headless'] # '--headless' if using chrome instead of firefox
通过使用此设置,它可以在我的本地机器上完美运行
chrome_path = "/Users/username/chromepath/chromedriver"
SELENIUM_DRIVER_NAME = 'chrome' # Change to your browser name
SELENIUM_DRIVER_EXECUTABLE_PATH = chrome_path
SELENIUM_DRIVER_ARGUMENTS=['--headless'] # '--headless' if using chrome instead of firefox
我在互联网上找到了一些解决方案,但它们只有在我使用 webdriver 获取请求时才有效。
chrome_options = Options()
chrome_options.binary_location = GOOGLE_CHROME_BIN
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
所以我现在的问题是如何设置scrapy-selenium 路径以在heroku 上使用我的环境
【问题讨论】:
-
您有可以发布的回溯或错误消息来帮助我们解决您的问题吗?
-
我认为我取得了一些进展我在我的设置中使用了以下设置。py
SELENIUM_DRIVER_NAME = 'chrome' # Change to your browser nameSELENIUM_DRIVER_EXECUTABLE_PATH = os.environ.get("GOOGLE_CHROME_BIN")SELENIUM_DRIVER_ARGUMENTS=['--headless','--no-sandbox','--disable-dev-shm-usage',] # '--headless' if using chrome instead of firefox错误行是:selenium.common.exceptions.WebDriverException:消息:服务/ app/.apt/opt/google/chrome/chrome 意外退出。状态码是:-5 -
@ThePyGuy 在我浏览了 Selenium 请求中间件文档 [github.com/clemfromspace/scrapy-selenium/blob/develop/… 但现在我卡在 chrome 意外退出后,该过程变得更加清晰。状态码是 -5
标签: python selenium heroku scrapy