python爬虫—Ubuntu18.04环境下的安装import、selenium、chrome、chromedriver

放假第一天开始学爬虫喽。
Linux(ubuntu18.04):
一、安装python
在Ubuntu18.04下是自动安装python的。如图:
在这里插入图片描述二、安装python–requests库
requests库的作用是什么呢?他的作用就是模拟浏览器向服务器发送请求。
同上requests库也是自带的。但是我们在使用requests却出现了这种情况,如下:

gxa@gxa-virtual-machine:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
/usr/lib/python3/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.25.3) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)

什么意思呢?
就是说:python库中urllib3 (1.24.1) or chardet (3.0.4) 的版本不兼容。所以我们执行一下命令:

~$pip uninstall urllib3
~$pip uninstall chardet

出现如下我们可以看到安装成功:

gxa@gxa-virtual-machine:~$ pip3 uninstall urllib3
Uninstalling urllib3-1.25.3:
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/INSTALLER
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/LICENSE.txt
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/METADATA
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/RECORD
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/WHEEL
  /home/gxa/.local/lib/python3.6/site-packages/urllib3-1.25.3.dist-info/top_level.txt
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/_collections.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/connection.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/connectionpool.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/exceptions.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/fields.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/filepost.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/poolmanager.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/request.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/__pycache__/response.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/_collections.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/connection.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/connectionpool.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/_appengine_environ.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/appengine.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/ntlmpool.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/pyopenssl.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/securetransport.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/__pycache__/socks.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_appengine_environ.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/__pycache__/bindings.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/__pycache__/low_level.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/bindings.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/_securetransport/low_level.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/appengine.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/ntlmpool.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/securetransport.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/contrib/socks.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/exceptions.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/fields.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/filepost.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/__pycache__/six.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/backports/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/backports/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/backports/__pycache__/makefile.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/backports/makefile.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/_mixin.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/abnf_regexp.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/api.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/builder.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/compat.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/exceptions.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/iri.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/misc.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/normalizers.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/parseresult.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/uri.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/__pycache__/validators.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/_mixin.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/abnf_regexp.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/api.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/builder.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/compat.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/exceptions.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/iri.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/misc.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/normalizers.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/parseresult.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/uri.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/rfc3986/validators.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/six.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/ssl_match_hostname/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/ssl_match_hostname/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/ssl_match_hostname/__pycache__/_implementation.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/packages/ssl_match_hostname/_implementation.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/poolmanager.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/request.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/response.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__init__.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/__init__.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/connection.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/queue.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/request.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/response.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/retry.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/ssl_.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/timeout.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/url.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/__pycache__/wait.cpython-36.pyc
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/connection.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/queue.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/request.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/response.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/retry.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/ssl_.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/timeout.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/url.py
  /home/gxa/.local/lib/python3.6/site-packages/urllib3/util/wait.py
Proceed (y/n)? y
  Successfully uninstalled urllib3-1.25.3 //这是重点奥。

如果没有requests库的话,我们可以使用以下命令:

pip install requests

关于pip这个命令,我们稍后介绍以下。
三、接下来我们就安装selenium库啦。
这个库的作用是什么呢?他是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,比如点击、下拉等操作。同样我们安装此库,也需要用到以下命令:

pip3 install selenium

但是呢我们在执行命令的时候发现并不能执行,我们就需要在Ubuntu中安装此命令,但是我们在安装的过程中,发现并不能安装成功。为什么会出现这种问题呢?
原因很简单,就是自带的源没有找到python3-pip这个包,所以我们要先添加下面的源:
**如何解决pip3安装不成功呢?**当然是换源啦。

# 打开源配置文件
sudo vim /etc/apt/sources.list  //这里要注意我们在退出保存的时候使用的命令是: :wq 而不是 :qw
 
 
# 在原有基础上添加以下的源
deb http://cn.archive.ubuntu.com/ubuntu bionic main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-proposed main multiverse restricted universe
 
 
# 更新一下源
sudo apt update

如图(前四个就是我们添加的源):
在这里插入图片描述安装完毕之后我们查看以下版本:

gxa@gxa-virtual-machine:~$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

然后我们执行以下命令,安装selenium库:
或者执行(sudo apt install python3-pip)
在这里插入图片描述完成以上步骤之后,我们执行以下命令奥:
在这里插入图片描述可以看到两个库已经安装成功啦。
我们之前已经是说过selenium的作用啦,所以这种库当然是要配合浏览器了啊。关于如何在ubuntu中安装goole-chrome?
四、接下来我们安装chromedriver
首先我们打开goole-chrome查看版本号,然后在chromedriver的官网上下载对应的版本。
在这里插入图片描述我们可以看到版本号是75.0.3440.100。那么我们就在官网下载相应版本。下载完毕之后我们:将文件移动到/usr/bin目录下,但是博主在移动的时候出现了以下情况:
在这里插入图片描述为什么文件一直没移动成功呢?那是因为我们虚拟机上根本就没有/home/gax/桌面/chromedriver折耳根文件目录。所以我改变以下移动方式。cd桌面移动此文件。
在这里插入图片描述最终文件被成功移动。
我们输入命令:chromedrier启动。
在这里插入图片描述ok啦。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值