ubuntu server搭建python+selenium

ubuntu+python+selenium

前言

写这篇博客之前,我在网上找了一堆ubuntu上搭建selenium+浏览器的环境,有chrome、Firefox、phantomjs。最后在重装了n次服务器后,搭建成功了,在这里记录一下。

selenium安装

传送门
这篇知乎是我见过的介绍selenium比较全的文章了,大家可以参考一下哈。

phantomjs安装

phantomjs官网

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar -xvf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mv phantomjs-2.1.1-linux-x86_64 /usr/local/phantomjs
ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/phantomjs

就这样子phantomjs就安装好了,可以利用phantomjs --version命令查看版本信息

在这里插入图片描述

想要利用selenium调用phantomjs的话不需要安装其他驱动了,直接在python中调用就行。

>>> from selenium import webdriver
>>> browser=webdriver.PhantomJS()     #JS要用大写
>>> browser.get("https://www.baidu.com")
>>> print(browser.current_url)
>>> browser.refresh()

chrome

下载最新版本的chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb

安装完成
可以使用google-chrome --version查看版本信息,也可以使用google-chrome命令直接调用谷歌浏览器。

在selenium中调用chrome需要下载Chromedriver,并放置在/usr/local/bin/文件夹中
下载地址

自动化安装脚本

#!bin/sh
echo deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse  > /etc/apt/sources.list
echo deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse  >> /etc/apt/sources.list
echo deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse  >> /etc/apt/sources.list
echo deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse  >> /etc/apt/sources.list
echo deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse  >> /etc/apt/sources.list
echo deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse  >> /etc/apt/sources.list
echo deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse  >> /etc/apt/sources.list
echo deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse  >> /etc/apt/sources.list
echo deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse  >> /etc/apt/sources.list
echo deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse  >> /etc/apt/sources.list
apt update
apt upgrade -y
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb -y
sudo apt install python3-pip
python -m pip install selenium
python -m pip install lxml

这个脚本实现了从换源到安装pip、selenium、安装chrome等。

你可能感兴趣的:(ubuntu server搭建python+selenium)