selenium自动化脚本之大麦网抢票


写在前面

最近一直向做一个自动化脚本抢票,于是在网上找一下资料,找到 DamaiHelper项目,这是一个用python实现的基于selenium自动化买票项目,作者花了点时间将它运行起来,运行过程中遇到了一些问题,在此记录一下

安装selenium包

作者一开始用的是pip install selenium安装,一直报错

➜  ~ pip install selenium
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

[notice] A new release of pip is available: 24.0 -> 24.1
[notice] To update, run: python3.12 -m pip install --upgrade pip
➜  ~

在网上查说是包管理工具之间的冲突,不太明白,看提示说加上--break-system-packages就可以了,加上之后确实安装selenium成功了,还能看到版本号,但运行程序时还是报错ModuleNotFoundError: No module named 'selenium'

经过查阅资料,作者发现安装的python版本是3.9.6,既然是python3就应该用pip3安装,于是用pip3重新安装pip3 install selenium --break-system-packages

注意,这里有一个坑,安装selenium成功后确实可以运行起来,但是报错,说self.driver = webdriver.Chrome(service=service, options=options, desired_capabilities=capa)中多了一个参数desired_capabilities,原因是selenium版本不对,pip3安装的时候没有指定版本,安装的是最新版(4.22.0),应该用4.10.0以下版本,作者用4.9.1可行。pip3 install selenium==4.9.1 --break-system-packages

下载chromedriver

selenium调用的是chrome浏览器,需要下载chrome驱动。这个驱动和电脑上安装的chrome版本必须一致,需要先查看安装的chrome版本


作者的chrome版本是125.0.6422.142,去https://chromedriver.storage.googleapis.com/index.html下载driver。


对于作者来说,这个网址作用不大,里面没有125版本,尝试下载最新的版本114.0.5735.90,但是没有用,最后还得在其他地方下载对于的版本。chromedriver125,作者已经将它放在项目中

运行

python3 main.py

即可看到程序启动chrome浏览器,第一层需要手动登录。

源代码

https://github.com/ZBIGBEAR/DamaiWang

参考

[1]chromedriver-for-chrome-version-125-macos

[2]DamaiHelper

[4]DamaiWang

br>


文章作者: Alex
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex !
  目录