Python 在没有GUI的Ubuntu服务器上使用Selenium和Chrome驱动程序
我想抓取一些动态url(页面是使用JavaScript构建的,并重定向到外部页面)。我知道我需要使用无头浏览器,我正在使用Selenium和Chrome驱动程序。以下代码符合我在Windows计算机上的要求:Python 在没有GUI的Ubuntu服务器上使用Selenium和Chrome驱动程序,python,selenium,scrapy,selenium-chromedriver,google-chrome-headless,Python,Selenium,Scrapy,Selenium Chromedriver,Google Chrome Headless,我想抓取一些动态url(页面是使用JavaScript构建的,并重定向到外部页面)。我知道我需要使用无头浏览器,我正在使用Selenium和Chrome驱动程序。以下代码符合我在Windows计算机上的要求: 从selenium导入webdriver 从webdriver_manager.chrome导入ChromeDriverManager 类MySpider(scrapy.Spider): name=‘我的蜘蛛’ 定义初始化(自): self.driver=webdriver.Chrome(
从selenium导入webdriver
从webdriver_manager.chrome导入ChromeDriverManager
类MySpider(scrapy.Spider):
name=‘我的蜘蛛’
定义初始化(自):
self.driver=webdriver.Chrome(ChromeDriverManager().install())
def解析(自我,响应):
link=“使用javascript.com的某个url”
self.driver.get(链接)
time.sleep(1)#如果没有此等待,driver.current_url不是最终重定向
url=self.driver.current\u url
但是当我在我的Ubuntu服务器(没有GUI)上运行相同的代码时,我得到以下错误:
builtins.ValueError:无法获取具有此属性的Chrome版本
命令:google chrome--version | | google chrome stable--version
我已经在Ubuntu服务器上安装了谷歌chrome stable和chrome chromedriver
我还尝试了以下代码:
类MySpider(scrapy.Spider):
name=‘我的蜘蛛’
定义初始化(自):
#self.driver=webdriver.Chrome(ChromeDriverManager().install())
#self.driver=webdriver.Chrome(“/usr/bin/chromedriver”)
chrome_options=options()
chrome_选项。添加_参数('--headless')
chrome_选项。添加_参数('--no sandbox')
chrome_选项。添加_参数('--disable dev shm usage')
self.driver=webdriver.Chrome('/usr/bin/chromedriver',Chrome\u options=Chrome\u options)
但是我得到了以下错误:
selenium.common.exceptions.WebDriverException:消息:未知错误:
Chrome启动失败:异常退出。(未知错误:
DevToolsActivePort文件不存在)(进程从
chrome location/usr/bin/google chrome不再运行,因此
ChromeDriver假设Chrome已经崩溃。)
我必须安装Xvfb(请参阅)。还要查看更多详细信息。