Python3 常用爬虫库的安装

目录

1 简介

2 requests库的安装

3 selenium库的安装

4 phantomjs浏览器的安装

5 lxml库的安装

6 beautifulsoup库的安装

7 pyquery库的安装

 8 pymysql库的安装

9 pymongo库安装

10 redis库的安装

11 flask库的安装

12 django库的安装

13 jupyter库的安装

14 scrapy框架安装

14.1 安装环境

14.2 wheel库安装

14.3 lxml库安装

14.4 pyOpenSSL库安装

14.5 Twisted库安装

14.6.pywin32库安装

14.7 scrapy库安装

15 Mongodb的安装和配置

15.1 Mongodb下载

15.2 Mongodb的启动

15.3 Mongodb的配置

15.4 Mongodb的可视化软件Robomongo

16 AppiumPythonClient库的安装


1 简介

Windows下安装Python3常用的爬虫库:requests、selenium、beautifulsoup4、pyquery、pymysql、pymongo、redis、flask、django、jupyter和scrapy框架

进入控制台,用pip3 list命令查看系统已经安装了哪些第三方包:

DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

 结果显示已经安装了pip (9.0.1)setuptools (28.8.0),并提示pip可以升级到9.0.3版本,这个可以不用管。

Tips:若安装了多个Python版本,各自安装的第三方包是独立的。本机安装了Python2.7Python3.6两个版本,命令行进入python修改为python2python3,对应的pip命令修改为pip2pip3,本机的第三方库全安装Python3.6版本下。


2 requests库的安装

这是Python用于网页请求的库,不是内置库,需要手动安装,可以用pip命令直接安装:

C:\Users\Strive>pip3 install requests

等待安装,没有意外,最后会提示requests库以及相关依赖库安装成功:

Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

测试一下requests库,命令行进入python,并获取百度首页:

C:\Users\Strive>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('http://www.baidu.com')
<Response [200]>

返回响应码200,说明requests库安装正常。


3 selenium库的安装

当请求用JS渲染的页面时,requests库是无法正常获取到页面结果的,这个时候就需要用到selenium库,该库可以驱动浏览器来获得JS渲染后的页面,可以用pip命令直接安装:

C:\Users\Strive>pip3 install selenium

等待安装,没有意外,最后会提示selenium库(没有依赖库)安装成功的信息:

Successfully installed selenium-3.11.0

测试一下selenium库,命令行进入python,驱动Chrome进入百度首页:

C:\Users\Strive>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
Traceback (most recent call last):
    File "D:\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
    File "D:\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
    File "D:\Python\Python36-32\lib\subprocess.py", line 992, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "D:\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
        self.service.start()
    File "D:\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
        os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

结果显示chromedriver没有配置到环境变量中,并给出了chromedriver的下载地址:

https://sites.google.com/a/chromium.org/chromedriver/home 因为本机没有FQ,这个地址打不开,上百度搜索镜像,并下载对应的Chrome浏览器驱动:http://npm.taobao.org/mirrors/chromedriver/ 下载最新的版本2.9,解压到当前文件夹,并将chromedriver.exe文件复制到D:\Python\Python36-32\Scripts目录,该目录已经配置到了环境变量中。然后用上述命令继续测试:

C:\Users\Strive>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()

DevTools listening on ws://127.0.0.1:12734/devtools/browser/7496d615-f493-4f91-ab7e-4a2ac81012b9

 这时会弹出一个空白的Chrome窗口。继续获取百度首页面:

>>> driver.get('http://www.baidu.com')

 结果报错:

        raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"E43AB8BE7AF397B6CDDD47425C8396BF","isDefault":true},"id":1,"name":"","origin":"://"}
    (Session info: chrome=65.0.3325.146)
    (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)

chromedriver版本与Chrome版本不匹配,chromedriver版本太高,重新下一个版本替换掉上一个chromedriver.exe,在notes.txt中可以查看chromedriver对应的Chrome版本,本机Chrome版本为版本 65.0.3325.146(正式版本) (64 位),下载chromedriver2.37版本,继续测试:

>>>driver.get('http://www.baidu.com')

 界面可以正常显示百度首页。接下来获取网页源码:

>>>driver.page_source

Tips:若下载的chromedriver版本不支持你的Chrome还会继续报错,请下载符合你Chrome的chromedriver。


4 phantomjs浏览器的安装

phantomjs是一个无界面的浏览器,在使用爬虫模拟浏览器操作的时候不会出现浏览器界面,操作非常方便。phantomjs的下载地址:http://phantomjs.org/download.html

直接下载最新版本即可。将解压后的phantomjs-2.1.1-windows目录剪切到D:\Python\Python36-32目录下,并将D:\Python\Python36-32\phantomjs-2.1.1-windows\bin目录配置到用户环境变量中去。命令行进入phantomjs命令:

C:\Users\Strive>phantomjs
phantomjs>

这个交互模式就可以执行js程序。接下来命令行进入python,用phantomjs请求百度首页,并获取网页源代码:

C:\Users\Strive>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.PhantomJS()
D:\Python\Python36-32\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
    warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
>>> driver.get('http://www.baidu.com')
>>> driver.page_source

能够成功获取百度首页代码,这个过程中并没有出现浏览器窗口,结果提示Selenium已经废弃了对PhantomJS的支持,请使用Chromeheadless模式。


5 lxml库的安装

lxml提供xpath的解析方式,可以对网页进行高效的解析。可以使用pip命令直接安装:

C:\Users\Strive>pip3 install lxml

等待安装,不出意外,会出现安装成功提示:

Successfully installed lxml-4.2.1

结果提示成功安装lxml-4.2.1版本。如果使用pip命令安装网速较慢,可以直接到:https://pypi.python.org/pypi/lxml 下载对应的lxml的.whl文件,然后用pip命令进行本地安装:

C:\Users\Strive>pip3 install file_path/file_name

Tips:需要下载和Python版本对应的lxml版本并且已经安装了whl库。


6 beautifulsoup库的安装

beautifulsoup同样是一个高效的网页解析库,并且其依赖于lxml库, 在安装beautifulsoup库前先安装好lxml库。可以直接使用pip命令直接安装:

C:\Users\Strive>pip3 install beautifulsoup4

beautifulsoup4表示beautifulsoup的第四个版本。等待安装,会出现安装成功提示:

Successfully installed beautifulsoup4-4.6.0

7 pyquery库的安装

pyquery库同样是一个网页解析库,可以使用pip命令直接安装:

C:\Users\Strive>pip3 install pyquery

等待安装,会出现安装成功提示:

Successfully installed cssselect-1.0.3 pyquery-1.4.0

 8 pymysql库的安装

pymysql是操作mysql数据库的第三方库,可以用pip命令直接安装:

C:\Users\Strive>pip3 install pymysql

等待安装,会出现安装成功提示:

Successfully installed pymysql-0.8.0

9 pymongo库安装

pymongo是操作Mongodb数据库的第三方库,可以用pip命令直接安装:

C:\Users\Strive>pip3 install pymongo

等待安装,会出现安装成功提示:

Successfully installed pymongo-3.6.1

10 redis库的安装

redis是一个非关系型数据库,以键值对的形式存储数据,在分布式爬虫中用于为维护一个公共爬取队列。可以用pip命令直接安装redis库:

C:\Users\Strive>pip3 install redis

 等待安装,会出现安装成功提示:

Successfully installed redis-2.10.6

11 flask库的安装

flask是一个web库,爬虫中用于代理的一些设置,设置web服务器,来设置代理的获取或者存储。flask的官网:http://flask.pocoo.org/docs/0.12/ 

可以用pip命令直接安装:

C:\Users\Strive>pip3 install flask

等待安装,该库有很多的依赖库需要安装,会出现安装成功提示:

Successfully installed Werkzeug-0.14.1 click-6.7 flask-0.12.2 itsdangerous-0.24

12 django库的安装

django是一个web服务器框架,提供完整的后台管理,接口和路由等。django的官网:https://docs.djangoproject.com/en/2.0/

pip命令直接安装:

C:\Users\Strive>pip3 install django

等待安装,出现安装成功提示:

Successfully installed django-2.0.3 pytz-2018.3

13 jupyter库的安装

jupyter是一个运行在网页端的notebook,支持代码编译和markdown语法,用pip命令直接安装:

C:\Users\Strive>pip3 install jupyter

等待安装,出现安装成功提示:

Successfully installed jupyter-1.0.0 jupyter-console-5.2.0 qtconsole-4.3.1

然后命令行输入:

C:\Users\Strive>jupyter notebook

或者:

C:\Users\Strive>ipython notebook

都可以启动jupyter


14 scrapy框架安装

14.1 安装环境

计算机系统64位,Python版本3.6.2 32位。

14.2 wheel库安装

用于安装.whl后缀文件,直接使用pip命令进行安装:

C:\Users\Strive>pip3 install wheel

14.3 lxml库安装

参考第5章lxml库的安装。

14.4 pyOpenSSL库安装

在地址:https://pypi.python.org/pypi/pyOpenSSL#downloads 下载pyOpenSSL-17.5.0-py2.py3-none-any.whl (md5, pgp)文件到本地,然后使用pip命令进行安装:

C:\Users\Strive>pip3 install path/pyOpenSSL-17.5.0-py2.py3-none-any.whl

Tips:需要完整的文件路径以及带上文件后缀名.whl

14.5 Twisted库安装

在地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted 下载Twisted‑17.9.0‑cp36‑cp36m‑win32.whl文件到本地,然后使用pip命令进行安装:

C:\Users\Strive>pip3 install path/Twisted‑17.9.0‑cp36‑cp36m‑win32.whl

 

Tips: cp36代表Python版本为3.6,win32代表Python为32位,并不是系统版本是32位,如果Python版本为64为可以下载win_amd64的版本。

14.6.pywin32库安装

在地址:https://sourceforge.net/projects/pywin32/files/pywin32/Build 220/ 下载pywin32-220.win32-py3.6.exe文件到本地,win32代表的是Python版本为32位,若Python版本为64位,可以下载win-amd64的版本。然后执行下载好的exe文件,该文件会自动识别Python的版本以及安装路径,这里下载的Python3.6 32位的版本,会自动识别出来,但是安装程序会报一个如下错误:

Python version 3.6 required, which was not found in the registry

提示在系统注册表中找不到3.6版本的Python,但实际上Python是已经安装好并能正常使用。这时需要在注册表将Python3.6的安装路径注册一下,win+r打开运行输入regedit代开注册表,找到HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore下面有一个名为3.6-32注册项目文件。若本机下载的是win-amd64版本的exe文件,是能通过这个注册项目文件识别出Python的安装路径的,但是安装完成后是不匹配的,无法正常使用。因为我们的Python版本是32位的。所以我们需要在3.6-32的同级目录下新建一个3.6注册项目文件,然后将3.6-32项目下的HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.6-32\InstallPathHKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.6-32\PythonPath注册项目复制到3.6注册项目文件中(我是手动一个一个新建的。。。),然后再运行pywin32-220.win32-py3.6.exe文件,就能正常识别Python3.6的安装路径了。然后进行正常安装就行。安装完成进入Python验证一下:

C:\Users\Strive>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32com
>>>

不报错,说明安装成功了。

14.7 scrapy库安装

在上述依赖库都安装成功后,使用pip命令直接安装scrapy库:

C:\Users\Strive>pip3 install scrapy

等待安装完成即可,然后输入scrapy出现如下提示:

C:\Users\Strive>scrapy
Scrapy 1.5.0 - no active project

Usage:
    scrapy <command> [options] [args]

Available commands:
    bench        Run quick benchmark test
    fetch        Fetch a URL using the Scrapy downloader
    genspider    Generate new spider using pre-defined templates
    runspider    Run a self-contained spider (without creating a project)
    settings     Get settings values
    shell        Interactive scraping console
    startproject Create new project
    version      Print Scrapy version
    view         Open URL in browser, as seen by Scrapy

    [ more ]     More commands available when run from project directory

Use "scrapy <command> -h" to see more info about a command

说明scrapy安装成功,可以正常使用。


15 Mongodb的安装和配置

15.1 Mongodb下载

15.2 Mongodb的启动

安装完Mongodb后,将D:\Python\MongoDB\Server\3.6\bin目录配置到系统环境变量中,就可以直接在控制台中直接使用bin目录下的可执行命令。但是此时在控制台中输入mongo命令,控制台提示连接Mongo失败,原因是安装完成后,没有配置数据存放位置。

15.3 Mongodb的配置

  • Mongodb的目录需求:Mongodb需要一个data\db目录来存放数据,一个logs目录来存放日志,一个etc目录来存放配置文件,这三个目录可以配置在任何地方,此时将logs目录配置为data的子目录,将data目录和etc目录配置在bin目录的同级目录下,也就是D:\Python\MongoDB\Server\3.6中。
  • Mongodb所需目录创建:
    • data目录: D:\Python\MongoDB\Server\3.6\data\db
    • logs目录: D:\Python\MongoDB\Server\3.6\data\logs\mongo.log
    • etc目录: D:\Python\MongoDB\Server\3.6\etc\mongo.config
  • 使用mongod命令配置数据库:
mongod --dbpath D:\Python\MongoDB\Server\3.6\data\db

 在控制台输入上述命令后按下回车,控制台显示:

I NETWORK [initandlisten] waiting for connections on port 27017

 说明Mongodb启动成功,可以用本地浏览器访问:

localhost:27017

 结果显示:

It looks like you are trying to access MongoDB over HTTP on the native driver port.

 本机测试上述输入未能成功显示,log显示,但命令行可正常访问数据库,该问题有待解决:

I NETWORK [conn1] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed.

该命终端窗口不要关闭,令起一个终端,在命令行输入命令:

mongo

 终端显示:

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
>

 成功访问数据库,就此Mongodb初步配置完成。

  • 使用mongod命令配置数据库为系统服务:
mongod --bind_ip 0.0.0.0 --dbpath D:\Python\MongoDB\Server\3.6\data\db --logpath D:\Python\MongoDB\Server\3.6\data\logs\mongo.log --logappend --port 27017 --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install

管理员身份打开终端,输入上述命令,回车,然后在系统服务中查看是否有名为MongoDB的服务,可设置为自动启动,那么每次开机就可以直接使用Mongodb数据库了。

  • 使用etc\mongo.config配置数据库为系统服务:
mongod --config D:\Python\MongoDB\Server\3.6\etc\mongo.config --serviceName="MongoDB" --serviceDisplayName "MongoDB" --install

管理员身份打开终端,在终端中输入上述命令,同样可以将Mongodb启动配置为系统服务。其中配置文件mongo.config内容为:

dbpath=D:\Python\MongoDB\Server\3.6\data\db
logpath=D:\Python\MongoDB\Server\3.6\data\logs\mongo.log
port=27017
logappend=true
bind_ip=0.0.0.0 

15.4 Mongodb的可视化软件Robomongo


16 AppiumPythonClient库的安装

使用PythonAppiumPythonClient库来操作Appium,此库继承Selenium,使用方法和Selenium有很多相似之处。

C:\Users\Strive>pip3 install Appium-Python-Client
Collecting Appium-Python-Client
  Downloading https://files.pythonhosted.org/packages/65/f4/b31229c97ecd03015f2e2abd79ee998f0b91977062d559270abda9f1f3fe/Appium-Python-Client-0.28.tar.gz
Requirement already satisfied: selenium>=2.47.0 in d:\python\python36-32\lib\site-packages (from Appium-Python-Client) (3.11.0)
Building wheels for collected packages: Appium-Python-Client
  Running setup.py bdist_wheel for Appium-Python-Client ... done
  Stored in directory: C:\Users\Strive\AppData\Local\pip\Cache\wheels\6c\66\5a\fdb958254e1879c10ae6b9c310672c147dd626de286b8a4900
Successfully built Appium-Python-Client
Installing collected packages: Appium-Python-Client
Successfully installed Appium-Python-Client-0.28

 

转载于https://www.cnblogs.com/strivepy/p/9683971.html#_label12

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值