本篇文章的目的有两个: 
 1.记录搭建爬虫环境的过程 
 2.总结爬虫项目的心得体会
一、系统环境 
 该方案在32位ubuntu10.04和64位centos6.9上面测试通过,所需要用到的软件如下: 
 1.ubuntu10.04或者centos6.9任选其一,下文主要以centos6.9来说明 
 2.pyspider源代码,可以从这里下载到http://download.csdn.net/detail/king_bingge/8582249,也可以从官 下载https://github.com/binux/pyspider 
 3.对于phantomjs,因为该爬虫项目中使用到flash,而最新版本中的phantomjs是去掉了flash插件支持的(当然这里是为了phantomjs更高的效率),所以我选择了保留了flash插件的phantomjs版本,可以从这里下载到http://download.csdn.net/detail/king_bingge/8582351 
 这里也可以http://www.ryanbridges.org/2013/05/21/putting-the-flash-back-in-phantomjs/ 
 4.当然,既然我们使用了pyspider,那么你的系统肯定要支持python,我系统的python版本为2.7.7,你可以从这里下载到http://download.csdn.net/detail/king_bingge/8582267 
 5.为了让phantomjs支持flash,那么你还需要下载一个flash插件,对于falsh插件,你可以从这里下载到http://download.csdn.net/detail/king_bingge/8582273。,当然,官 或许是一个更好的选择。
到这里为止,我们所需要的主要软件都已经具备了,下面就开始进行环境的搭建
二、安装phantomjs
1.在安装phantomjs之前,先要安装依赖的环境
2.然后执行 rpm -ivh phantomjs-1.9-1.x86_64.rpm,完成之后,执行下面:
能看到版本信息
3.然后执行下面的命令,查看命令行参数:
我们能够看到有一个–load-plugins=参数,这就是我们家在插件所需要带上的参数。 
 4.千万别忘了还有一点,我们的flash插件还没有安装 
 解压:tar -xf install_flash_player_11_linux.x86_64.tar.gz 
 cp libflashplayer.so /usr/lib/mozilla/plugins 
 cp -R ./usr/* /usr 
 这样即可成功安装。可以使用adobe flash player 了。
5.那么如何测试我们安装的phantomjs 是否成功了呢面给出一个测试脚本:
将上面内容保存为test.js 
 然后执行命令: ./phantomjs –load-plugins=yes test.js 
 查看一下:当前目录下面是不是有video.png的截图呀,这就是flash播放时候的截图。 
 至此,你已经完成了phantomjs 的安装 
 更多详细的内容参考:http://www.ryanbridges.org/2013/05/21/putting-the-flash-back-in-phantomjs/ 
 三、安装pyspider 
 接下来开始进入正题了,安装pyspider 
 1.后续我们安装pyspider所需要的包都通过pip来进行安装,所以在这这钱我们要确保系统已经安装了pip,如果没有安装的话,现在pip源代码,进行安装 
 代码可以从这里下载到:https://pypi.python.org/packages/source/p/pip,也可以从这里下载到:http://download.csdn.net/detail/king_bingge/8582399 
 进行安装
[root@localhost pyspider-master]# pip install -r requirements.txt –allow-external mysql-connector-python 
 如果遇到安装libxml 错:则先安装下面两个依赖包: 
 yum install libxslt-devel libxml2-devell
pip install pyspider
使用 PycURL 时出现:
ImportError: pycurl: libcurl link-time ssl backend (nss) is different from compile-time ssl backend (none/other) 
 解决:
pip uninstall pycurl 
 export PYCURL_SSL_LIBRARY=[openssl|gnutls|nss] #错误中显示是 nss 则换成 nss 
 pip install pycurl
phantomjs: cannot connect to X server
Xvfb :2 -screen 0 800x600x24 2> /dev/null & 
 export DISPLAY=:2.0
一、出现的问题 
 Collecting pycurl (from pyspider) 
 Downloading pycurl-7.19.5.1.tar.gz (142kB) 
 100% |…………….| 143kB 88kB/s 
 Complete output from command python setup.py egg_info: 
 Traceback (most recent call last): 
 File “”, line 20, in 
 File “/tmp/pip-build-JT2wTm/pycurl/setup.py”, line 634, in 
 ext = get_extension(split_extension_source=split_extension_source) 
 File “/tmp/pip-build-JT2wTm/pycurl/setup.py”, line 392, in get_extension 
 ext_config = ExtensionConfiguration() 
 File “/tmp/pip-build-JT2wTm/pycurl/setup.py”, line 65, in init 
 self.configure() 
 File “/tmp/pip-build-JT2wTm/pycurl/setup.py”, line 100, in configure_unix 
 raise ConfigurationError(msg) 
 main.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory
Command “/root/.pyenv/versions/2.7.9/bin/python2.7 -c “import setuptools, tokenize;file=’/tmp/pip-build-jz728r/lxml/setup.py’; 
 exec(compile(getattr(tokenize, ‘open’, open)(file).read().replace(‘rn’, ‘n’), file, ‘exec’))” install –record 
 /tmp/pip-4ZQoWo-record/install-record.txt –single-version-externally-managed –compile” failed with error code 1 in /tmp/pip-build-jz728r/lxml 
 解决方法 
 yum install libxml2-devel libxslt-devel libcurl-devel
二、出现的问题 
 ImportError: pycurl: libcurl link-time ssl backend (nss) is different from compile-time ssl backend (none/other) 
 解决方法 
 pip uninstall pycurl 
 export PYCURL_SSL_LIBRARY=nss 
 pip install pycurl
三、出现的问题 
 ImportError: No module named mysql.connector 
 解决方法 
 pip install -r requirements.txt –allow-external mysql-connector-python 
 “`
后续会讲解一个实例:使用该环境实现一个建议爬虫项目。
文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树 络爬虫pyspider框架的使用208427 人正在系统学习中
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!