Python爬虫:Scrapy框架安装配置

本篇文章中Python的安装是基于window平台,安装Scrapy之前确保已经安装好下列程序:

安装Python

安装pywin32

安装pip

  • pip是用来安装其他必要包的工具,首先下载get-pip.py
  • 下载好之后,选中该文件夹所在路径,执行命令python get-pip.py
  • 执行命令后便会安装好pip,同时,它帮你安装了setuptools
  • 安装完了之后在命令行执行pip --version
  • 安装完后若提醒版本低,可进行更新python -m pip install --update pip
  • pip is already installed if you’re using Python 2 >=2.7.9 or Python 3 >=3.4

安装pyOPENSSL

  • 在Windows下,是没有预装pyOPENSSL的,而在Linux下是已经安装好的。
  • 安装地址:https://launchpad.net/pyopenssl (下载pyOpenSSL-0.11.winxp32-py2.7)
  • 如果出现了Python Version 2.7 required which was not found in the registry错误!
  • 解决方法:新建一个register.py 文件,把以下代码贴进去,保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __name__ == "__main__":
RegisterPy()
  • 在cmd 中运行python脚本
    register

  • 双击安装 pyOpenSSL-0.11.winxp32-py2.7
    register

安装 lxml

第一种办法:
  • lxml的详细介绍 link ,是一种使用 Python 编写的库,可以迅速、灵活地处理 XML
  • 直接执行如下命令pip install lxml就可完成安装
  • 如果提示 Microsoft Visual C++库没安装,则 link 下载支持的库。
  • 可能报错:
    lxml
第二种办法:

步骤1:安装wheel,cmd命令行运行:pip install wheel
步骤2:在这里http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml下载对应的.whl文件
Ctrl + F,输入lxml,找到下面这段,cp后面是Python的版本号,27表示2.7。

lxml‑3.6.4‑cp27‑cp27m‑win32.whl
lxml‑3.6.4‑cp27‑cp27m‑win_amd64.whl
lxml‑3.6.4‑cp34‑cp34m‑win32.whl
lxml‑3.6.4‑cp34‑cp34m‑win_amd64.whl
lxml‑3.6.4‑cp35‑cp35m‑win32.whl
lxml‑3.6.4‑cp35‑cp35m‑win_amd64.whl
lxml‑3.6.4‑cp36‑cp36m‑win32.whl
lxml‑3.6.4‑cp36‑cp36m‑win_amd64.whl

步骤3:进入.whl所在的文件夹,执行命令pip install 带后缀的完整文件名即可完成安装
pip安装报错:is not a supported wheel on this platform
lxml_install
解决方法:在shell中输入import pip; print(pip.pep425tags.get_supported())可以获取到pip支持的文件名还有版本

1
2
3
4
5
6
7
8
import pip; print(pip.pep425tags.get_supported())
[('cp27', 'none', 'win_amd64'), ('cp27', 'none', 'any'), ('cp2', 'none', 'any'),
('cp26', 'none', 'any'), ('cp25', 'none', 'any'), ('cp24', 'none', 'any'),
('cp23', 'none', 'any'), ('cp22', 'none', 'any'), ('cp21', 'none', 'any'),
('cp20', 'none', 'any'), ('py27', 'none', 'any'), ('py2', 'none', 'any'),
('py26', 'none', 'any'), ('py25', 'none', 'any'), ('py24', 'none', 'any'),
('py23', 'none', 'any'), ('py22', 'none', 'any'), ('py21', 'none', 'any'),
('py20', 'none', 'any')]

可以发现上面下载的文件名格式是不支持的,修改为:lxml-3.7.3-cp27-none-win_amd64.whl即可成功安装。

安装Scrapy

  • 最后就是激动人心的时刻啦,上面的铺垫做好了,我们终于可以享受到胜利的果实啦!
  • 执行如下命令pip install Scrapy
  • pip 会另外下载其他依赖的包,这些就不要我们手动安装啦,等待一会,大功告成!
  • 验证安装,输入 Scrapy

创建项目

  • scrapy startproject phone
  • 发生问题:
    ssl
  • 问题原因:twist版本问题,twist版本高于需要的
  • 输入pip freeze查看twist版本,用pip install twisted==13.1.0 替代更高的版本
吃不起茶叶蛋了,求支持~
Fork me on GitHub