小言_互联网的博客

Ubuntu线上无界面服务器 使用selenium chrome

367人阅读  评论(0)

一, Ubuntu线上服务器 安装Chrome

sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb    # Might show "errors", fixed by next line
sudo apt-get install -f
google-chrome --version      # 查看版本

二,安装selenium

pip install selenium

三, 安装chromdriver

  • 进入 淘宝镜像源 下载chromdirver

    找到与你的chrome对应的版本, 下载linux版本就可以了

    例如: 我的Chrome的版本是77.0.3865

    找到版本 77.0.3865 下载对应的linux版本

  • 解压chromedriver_linux64.zip

  • 把解压得到的chromedirver 文件放到/usr/bin/ 下

  • 添加chromedirver可执行权限

    sudo chmod +x chromedriver
    

四, 测试是否成功

from selenium import webdriver
 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/chromedriver')    # 如果没有把chromedriver加入到PATH中,就需要指明路径
 
client.get("https://www.baidu.com")
print(client.page_source.encode('utf-8'))
 
client.quit()

​ 成功打印网页内容, 就成功了


转载:https://blog.csdn.net/qq_40117912/article/details/101860873
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场