如何在Win10上安装爬虫框架Scrapy

【版权申明】非商业目的注明出处可自由转载
博文地址:https://blog.csdn.net/ShuSheng0007/article/details/107358726
出自:shusheng007

概述

最近需要一些数据,就想自己写个爬虫,随即查了一下相关的工具,将准备环境的过程记录下来,以便自己和他人将来查阅。

安装Python3

官网下载最新版本,建议安装64位版本

注意:如果直接点击下载的大按钮下载下来的是32位版本

安装VS Code

由于要写python代码,建议使用VS Code。成功安装后,下载Python扩展程序,其实就是我们常说的插件。

安装Scrapy

由于版本冲突问题,Python提倡使用虚拟环境,所以我们准备将Scrapy安装在虚拟环境中

创建虚拟环境

先了解几个工具

  • vevn
    python3 使用来管理虚拟环境,其随着python3的安装而安装
  • pip
    python 依赖管理器,可以使用它来管理各种python库,例如安装和卸载,升级等。

升级Pip命令: python -m pip install --upgrade pip

开始创建:

  1. 打开VsCode的终端(Terminal),其默认使用windows的powershell

  2. 导航到要创建虚拟环境的路径下,执行如下命名创建虚拟环境

    python -m venv spider-venv
    

    spider-venv 是你要创建的虚拟环境的名称,可以任意命名。 成功后vscode左侧会出现一个spider-venv文件夹,里面有很多文件。

    如果机器上还装了python2,那么python换成python3

  3. 激活此虚拟环境

    ./spider-venv/Scripts/activate.ps1
    

    如果powershell出现了如下显示,说明成功:

    (spider-venv) PS xxxxxxxxxxxxxxxxx> 
    

    括号里面是你刚创建的虚拟环境名称

    如果报错,说明你powershell权限不够,去修改powershell的权限

    以管理员身份运行powershell,执行如下命令即可。具体可以参考 powershell 无法执行ps1文件解决方法

    set-executionpolicy remotesigned
    

    再次执行激活命令

    ./spider-venv/Scripts/activate.ps1
    

    小知识:退出虚拟环境的命令为: deactivate

  4. 切换python解释器

    自动方式:当激活虚拟环境时,如果vs弹出了让你使用虚拟环境的弹窗时,点击允许即可。

    手动方式:你也可以点击vscode左下角的Python xxxx 自己去切换,不推荐。

安装scrapy依赖

正常情况下,使用如下命令即可,你可以先尝试执行一下

pip install scrapy 

其会自动下载依赖并安装,但是其中有一个处理网络的依赖的库Twisted有可能在线安装不成功(取决于你的环境,如果你电脑上安装了visual c++的编译工具也许可以成功),如下所示
在这里插入图片描述
所以需要手动安装。

  1. 进入http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted ,下载对应的版本
    我的python版本是Python3.8.4-64bit,所以我就选择 Twisted-20.3.0-cp38-cp38-win_amd64.whl
  2. 安装
    pip install D:\xxxxx\Twisted-20.3.0-cp38-cp38-win_amd64.whl
    

安装scrapy

	pip install Scrapy

等待安装完成即可,成功后输入类似内容:

...
Requirement already satisfied: lxml>=3.5.0 in d:\program files\python3\lib\site-packages (from Scrapy) (4.5.2)
Requirement already satisfied: cryptography>=2.0 in d:\program files\python3\lib\site-packages (from Scrapy) (2.9.2)
Requirement already satisfied: Twisted>=17.9.0 in d:\program files\python3\lib\site-packages (from Scrapy) (20.3.0)
Requirement already satisfied: six>=1.6.0 in d:\program files\python3\lib\site-packages (from parsel>=1.5.0->Scrapy) (1.15.0)
Requirement already satisfied: attrs>=16.0.0 in d:\program files\python3\lib\site-packages (from service-identity>=16.0.0->Scrapy) (19.3.0)
Requirement already satisfied: pyasn1 in d:\program files\python3\lib\site-packages (from service-identity>=16.0.0->Scrapy) (0.4.8)
Requirement already satisfied: pyasn1-modules in d:\program files\python3\lib\site-packages (from service-identity>=16.0.0->Scrapy) (0.2.8)
Requirement already satisfied: setuptools in d:\program files\python3\lib\site-packages (from zope.interface>=4.1.3->Scrapy) (41.2.0)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in d:\program files\python3\lib\site-packages (from cryptography>=2.0->Scrapy) (1.14.0)
Requirement already satisfied: constantly>=15.1 in d:\program files\python3\lib\site-packages (from Twisted>=17.9.0->Scrapy) (15.1.0)
Requirement already satisfied: hyperlink>=17.1.1 in d:\program files\python3\lib\site-packages (from Twisted>=17.9.0->Scrapy) (19.0.0)
Requirement already satisfied: Automat>=0.3.0 in d:\program files\python3\lib\site-packages (from Twisted>=17.9.0->Scrapy) (20.2.0)
Requirement already satisfied: incremental>=16.10.1 in d:\program files\python3\lib\site-packages (from Twisted>=17.9.0->Scrapy) (17.5.0)
Requirement already satisfied: PyHamcrest!=1.10.0,>=1.9.0 in d:\program files\python3\lib\site-packages (from Twisted>=17.9.0->Scrapy) (2.0.2)
Requirement already satisfied: pycparser in d:\program files\python3\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=2.0->Scrapy) (2.20)
Requirement already satisfied: idna>=2.5 in d:\program files\python3\lib\site-packages (from hyperlink>=17.1.1->Twisted>=17.9.0->Scrapy) (2.10)
Using legacy setup.py install for protego, since package 'wheel' is not installed.
Installing collected packages: itemadapter, queuelib, pyOpenSSL, protego, Scrapy
    Running setup.py install for protego ... done
Successfully installed Scrapy-2.2.0 itemadapter-0.1.0 protego-0.1.16 pyOpenSSL-19.1.0 queuelib-1.5.0

至此Scrapy就安装成功啦。

总结

现在中国人对数据的重视程度越来越高了,所以使用网络爬虫时请注意相关法律法规问题,切不可触犯国家的相关法律,要避免面向监狱编程这种事发生在你的身上。

愿广大程序员们岁月静好,现世安稳

猜你喜欢

转载自blog.csdn.net/ShuSheng0007/article/details/107358726