• Python之selenium环境设置(linux版[centos])


    刚刚看到上一篇写mac的记录差不多两年了,两年的时间,虽然学的东西不少,感觉进步还是慢了写。

    这次为了前面一篇的获取头信息或者cookies,需要在我的centos的linux机器上部署环境。

    首先,需要在服务器上安装chrome浏览器的软件

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    

      

    通过wget的命令,下载chrome的rpm版本,这个是从官网下载了的地址

    这个地址,是我从网上查询到的一个百度网盘的可用历史版本的chrome浏览器比较全的安装包大全。

    百度网盘(含最新及历史版本):https://pan.baidu.com/s/195fycw8BEK4B1xhi4ig1Sg 密码: oig4

    下载完成后,直接通过yum命令安装。

    yum install -y google-chrome-stable_current_x86_64.rpm
    

      

    安装完成后,可以通过指定路径查看chrome的版本信息

    /opt/google/chrome/chrome --version
    Google Chrome 92.0.4515.107 unknown
    

      

    安装好chrome之后,我们可以更具chrome的版本信息,去网站下载chromedriver插件

    网址https://chromedriver.storage.googleapis.com/index.html

    通过wget下载指定的chromedriver之后,需要通过unzip解压该文件

    wget https://chromedriver.storage.googleapis.com/92.0.4515.43/chromedriver_linux64.zip
    

      

    解压之后,可以看到chromedriver的可执行文件,这里其实已经完成了环境的安装。

    后续如果有需要,可以将chromedriver复制到系统的环境中。比较简单的是通过echo $PATH查看系统的环境路径。

    我就是将chromedriver复制到了系统环境最后一个目录中,这样就可以通过chromedriver --version来看chromedriver的版本信息。

    chromedriver --version
    ChromeDriver 92.0.4515.43 (8c61b7e2989f2990d42f859cac71319137787cce-refs/branch-heads/4515@{#306})
    

      

    最后pip install selenium,这个就不说了,easy。

    这段测试代码,直接复制粘贴,如果运行输出结果,说明环境部署成功。

    #!/usr/bin/env python
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument('--headless') 
    # chrome_options.add_argument('--disable-gpu')
    # chrome_options.add_argument('--no-sandbox')  # root用户不加这条会无法运行
    
    driver = webdriver.Chrome(options=chrome_options)
    for i in range(10):
        driver.get("https://www.baidu.com/")
        print(driver.title)
    driver.close()
    

      

      

  • 相关阅读:
    c++ struct 使用
    c++数组、字符串操作
    c++ List、Vector、Stack、Queue使用
    十三、哈希表
    十二、234树
    十一、红黑树
    十、哈夫曼编码
    九、二叉树
    八、高级排序
    七、递归
  • 原文地址:https://www.cnblogs.com/sidianok/p/15055265.html
Copyright © 2020-2023  润新知