在Windows XP系统安装搭建Python2.7+Scrapy-1.6.0编程环境并创建一个爬虫项目的全过程

在Windows XP系统安装搭建Python2.7+Scrapy-1.6.0编程环境并创建一个爬虫项目的全过程

因为Note that Python 3.5.0 cannot be used on Windows XP or earlier.
因为想在Windows XP 系统下搭建Scrapy环境,经过考虑选择安装python-2.7
参考CSDN @GeraldJones和@嘘木的安装教程,经历数坑终于成功安装。

一、安装Python2.7
进入
https://www.python.org/
选择Windows下载版本
https://www.python.org/downloads/windows/

https://www.python.org/downloads/release/python-2716/

选择python-2.7.16 Windows x86 MSI installer (32位)版本下载进行安装

二、安装scrapy
(一)安装pywin32
https://sourceforge.net/projects/pywin32/files/latest/download
Python for Windows Extensions Overview
OLD project page for the Python extensions for Windows
This project has been migrated to github - please visit https://github.com/mhammond/pywin32

Please file all issues via github.
https://github.com/mhammond/pywin32/releases
找到与自己python版本对应的pywin32,@GeraldJones选择pywin32-221.win32-py2.7.exe,我选择pywin32-224.win32-py2.7.exe,点击安装,默认设置安装即可

(二)安装pyOpenSSL
在Windows下,是没有预装pyOPENSSL的,而在Linux下是已经安装好的。
下载地址:https://launchpad.net/pyopenssl
选择pyOpenSSL-0.11.winxp32-py2.7.exe,直接下一步(next)一路装完(必须在安装Python2.7安装后再安装,否则出现Python version 2.7 required 。不要选择pyOpenSSL-0.11.winxp32-py2.7需要自己设置安装目录provide an alternate Python location 默认是F:\PythonX\)

安装完后在Windows shell 输入 pip list,
显示:
Package version
Pip 18.1
PyopenSSL 0.11
Pywin32 224
setuptool 40.6.2

(三)下载并安装lxml(whl文件)
网站一:https://www.lfd.uci.edu/~gohlke/pythonlibs/
从网站下载下来
lxml-4.3.4-cp27-cp27m-win32.whl(1.72M)
网站二:https://pypi.org/project/lxml/#files
lxml-4.3.4-cp27-cp27m-win32.whl(3.08M)

将从pypi下载的 lxml-4.3.4-cp27-cp27m-win32.whl(3.08M)放入Python27\Lib目录下
然后用点击开始->运行->输入cmd进入Windows shell(命令行)
进入Python27\Lib目录下:使用pip install lxml-4.3.4-cp27-cp27m-win32.whl安装

(四)下载并安装scrapy(whl文件)
https://scrapy.org/download/
https://pypi.org/project/Scrapy/
https://pypi.org/project/Scrapy/#files
选择下载Scrapy-1.6.0-py2.py3-none-any.whl (231.8 kB)
将下载的Scrapy-1.6.0-py2.py3-none-any.whl (231.8 kB)
放入Python27\Lib目录下
然后用点击开始->运行->输入cmd进入Windows shell(命令行)
进入Python27\Lib目录下:使用pip install Scrapy-1.6.0-py2.py3-none-any.whl 安装。

(五)安装visual c++ 9.0
安装Scrapy-1.6.0-py2.py3-none-any.whl 会报错,提示:error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

打开http://aka.ms/vcpython27会跳转到http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266
将VCForPython27.MSI 一路安装后再重新进入Python27\Lib目录下:使用pip install Scrapy-1.6.0-py2.py3-none-any.whl 安装。
Successfully installed Scrapy-1.6.0 Twisted-19.2.1 functiontools32-3.2.3.post2 parse1-1.5.1。 显示安装成功。

Python27\Lib使用pip list 命令,如图所示:
PyopenSSL 0.11
Twisted 19.2.1
再输入Scrappy
显示:Scrapy-1.6.0 - no active project
并且没有crawl 命令!(根据前人经验说明安装有问题)

三、根据Scrapy入门教程 创建第一个scrapy项目
(一)创建Scrapy项目代码
代码参考:https://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html

输入c:\scrapy startproject tutorial

该命令将会创建包含下列内容的 tutorial 目录:
tutorial/
scrapy.cfg
tutorial/
init.py
items.py
pipelines.py
settings.py
spiders/
init.py

编辑 tutorial 目录中的 items.py 文件:
import scrapy

class DmozItem(scrapy.Item):
title = scrapy.Field()
link = scrapy.Field()
desc = scrapy.Field()

编写第一个Spider代码,保存在 tutorial/spiders 目录下的 dmoz_spider.py 文件中:
import scrapy

class DmozSpider(scrapy.Spider):
name = “dmoz”
allowed_domains = [“dmoz.org”]
start_urls = [
http://www.dmoz.org/Computers/Programming/Languages/Python/Books/”,
http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/
]

def parse(self, response):
    filename = response.url.split("/")[-2]
    with open(filename, 'wb') as f:
        f.write(response.body)

(二)启动爬虫
进入项目的根目录c:\tutoria,执行下列命令启动spider:
scrapy crawl dmoz

出现 from OpenSSL.util import lib as pyOpenSSL
ImportError:No module name _util错误。

四、寻找解决方案

根据CSDN@嘘木
Hello Scrapy 踩坑 ImportError: No module named _util
https://blog.csdn.net/kazeki/article/details/81751296
Twisted调用OpenSSL时出错,那么就是这两个不匹配,那就是拿这两个开刀。
回退到什么版本?机智如我,这时候还是要去官网看看啊!
https://doc.scrapy.org/en/latest/intro/install.html#intro-install
The minimal versions which Scrapy is tested against are:

  • Twisted 14.0
  • lxml 3.4
  • pyOpenSSL 0.14
    就是它了。
    于是卸载并重装了pyOpenSSL ,
    在c:\python27\lib目录
    pip uninstall pyopenssl

出现红字:Cannot uninstall ‘pyOpenSSL’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall。

只能在C盘中搜索pyOpenSSL所有文件进行手动删除:
安装完后在Windows shell 输入 pip list,已无pyOpenSSL。

然后重新安装pip install pyopenssl==0.14,成功。

试着进入项目的根目录c:\tutoria,执行下列命令启动spider:
scrapy crawl dmoz

出现错误:from cryptography.hazmat.bindings._openssl import ffi,lib
ImportError:DLL load failed:找不到指定的模块。

于是接着重装Twisted ,
在Windows shell输入:
pip uninstall Twisted
pip install Twisted==14.0

进入项目的根目录c:\tutoria,
执行下列命令启动spider:
scrapy crawl dmoz再试,终于成功了!

参考资料:
1.Python(2.7版本)安装scrapy
https://blog.csdn.net/Gerald_Jones/article/details/78806066
2.Scrapy入门教程
https://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html
3.Hello Scrapy 踩坑 ImportError: No module named _util
https://blog.csdn.net/kazeki/article/details/81751296

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值