python+pyspider+phantomjs实现简易爬虫功能

本篇文章的目的有两个:
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之前,先要安装依赖的环境

yum install -y xorg-x11-server-Xvfb xorg-x11-server-Xorg xorg-x11-fonts* dbus-x11 xulrunner.x86_64 nspr.x86_64 nss.x86_64
yum install -y  flash-plugin nspluginwrapper libcurl

2.然后执行 rpm -ivh phantomjs-1.9-1.x86_64.rpm,完成之后,执行下面:

[root@localhost pyspider]# phantomjs  -v
1.10.0 (development)
[root@localhost pyspider]# 

能看到版本信息

3.然后执行下面的命令,查看命令行参数:

[root@localhost pyspider]# phantomjs  -h
Usage:
   phantomjs [switchs] [options] [script] [argument [argument [...]]]

Options:
  --cookies-file=<val>                 Sets the file name to store the persistent cookies
  --config=<val>                       Specifies JSON-formatted configuration file
  --debug=<val>                        Prints additional warning and debug message: 'true' or 'false' (default)
  --disk-cache=<val>                   Enables disk cache: 'true' or 'false' (default)
  --ignore-ssl-errors=<val>            Ignores SSL errors (expired/self-signed certificate errors): 'true' or 'false' (default)
  --load-images=<val>                  Loads all inlined images: 'true' (default) or 'false'
  --load-plugins=<val>                 Loads all plugins: 'true' (default) or 'false'

我们能够看到有一个–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 是否成功了呢?下面给出一个测试脚本:

var page = require('webpage').create();
page.open('http://www.dhs.state.il.us/accessibility/tests/flash/video.html', function () {
    window.setTimeout(function(){
        page.render('video.png');
        phantom.exit();
    },10000);
});

将上面内容保存为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
进行安装

如果是centos先预装:
yum install  libxml2-devel   libxslt-devel libcurl-devel
#解压
tar -zxvf pip-6.1.1.tar.gz
cd pip-6.1.1
#安装
python setup.py install
这个时候会报错说少了setuptools
 wget https://bootstrap.pypa.io/ez_setup.py -O - | python
进行安装```
2.安装pip完成之后,执行命令

[root@localhost pyspider-master]# pip install -r requirements.txt –allow-external mysql-connector-python
如果遇到安装libxml报错:则先安装下面两个依赖包:
yum install libxslt-devel libxml2-devell

3.之后在执行

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


4.运行./run.py,访问 http://192.168.1.1:5000/  ,看到如下界面就说明你大功告成了:
或者:pyspider --phantomjs-proxy="localhost:25555" -c config.ini

![成功后显示界面](https://img-blog.csdn.net/20150410192417418)

到这里为止,环境搭建完毕

注意:运行的时候如果遇到这个问题:

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

yum install libcurl-devel

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(‘\r\n’, ‘\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
“`

后续会讲解一个实例:使用该环境实现一个建议爬虫项目。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值