搭建ubuntu+firefox+jupyter+python3+selenium的环境

xiaoxiao2021-02-28  12

由于最近经常会有一些需要在网页上进行周期性处理的工作,所以就起了写个脚本来进行自动处理的念头。查了很多资料,最后发现python+selenium可以很好地满足我的需求。加上之前接触过jupyter notebook,觉得很好用。最后决定先搭建一套ubuntu+firefox+jupyter+python3+selenium的环境。

安装ubuntu

很简单,用VMware安装就行了。

安装openSSH

为了方便远程管理,需要安装SSH服务。

sudo apt update sudo apt install -y openssh-server

安装最新版的Firefox

ubuntu自带的firefox是51版本的,selenium需要53以上的版本的版本,需要升级一下。

sudo apt -y install firefox

查看firefox版本

firefox --version Mozilla Firefox 57.0

安装selenium

selenium只能通过pip安装,所以需要先安装pip

sudo apt -y install python3-pip

从清华镜像安装selenium

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium

验证是否安装成功并查看版本

python3 import selenium help(selenium) Help on package selenium: NAME selenium DESCRIPTION # Licensed to the Software Freedom Conservancy (SFC) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The SFC licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. PACKAGE CONTENTS common (package) webdriver (package) VERSION 3.7.0 FILE /home/selenium/.local/lib/python3.5/site-packages/selenium/__init__.py

selenium安装成功后还要安装firefox驱动

驱动下载地址:https://github.com/mozilla/geckodriver/releases 下载完成后将驱动解压到/usr/bin

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz tar -xzf ./geckodriver-v0.19.1-linux64.tar.gz sudo mv ./geckodriver /usr/bin/

测试selenium能否正常启动firefox

python3 from selenium import webdriver test = webdriver.Firefox()

安装jupyter notebook

同样需要使用pip安装

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter

配置jupyter notebook远程访问

生成配置文件

jupyter notebook --generate-config

设置密码

ipython from notebook.auth import passwd passwd()

把生成的密文‘sha:ce…’复制下来

Out[2]: 'sha1:e71cc9a18431:f65****eccd3****84***********'

修改默认配置文件

vi ./.jupyter/jupyter_notebook_config.py c.NotebookApp.ip='*' c.NotebookApp.password = u'sha:ce...刚才复制的那个密文' c.NotebookApp.open_browser = False c.NotebookApp.port =6789便指定一个端口

启动jupyter notebook

jupyter notebook --allow-root

后台启动

nohup sudo jupyter notebook --allow-root

至此,ubuntu+firefox+jupyter+python3+selenium的环境搭建完成。在浏览器中输入http://ip:6789,即可访问jupyter notebook。

转载请注明原文地址: https://www.6miu.com/read-2400282.html

最新回复(0)