pyspider在Linux的安装和基本使用

安装pyspider

python==2.7

升级pip
pip install --upgrade pip
安装pyspider
pip install pyspider
安装依赖
yum install bzip2
yum install fontconfig
yum install curl
pip install mysql-connector
pip install redis
开放端口

phantomjs 25555

pyspider 5000

安装phantomjs

https://phantomjs.org/download

# 下载
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
# 解压
tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C  /usr/local/src/
# 重命名
mv phantomjs-2.1.1-linux-x86_64/ phantomjs
# 软链接
ln -s /usr/local/src/phantomjs/bin/phantomjs /usr/bin/
# 启动验证
phantomjs

如果报错

ValueError: Invalid configuration:
  - Deprecated option 'domaincontroller': use 'http_authenticator.domain_controller' instead.

找到pyspider/webui/webdav.py, 修改第209行

'domaincontroller': NeedAuthController(app),
||修改为
'http_authenticator':{
        'HTTPAuthenticator':NeedAuthController(app),
    },
新建数据库

在mysql创建taskdb, projectdb, resultdb三个数据库
新建用户并且添加数据库权限

pyspider配置文件
# vi pyspider.config.json
{
    "taskdb": "mysql+taskdb://{user}:{passwd}@{ip}:3306/taskdb",
    "projectdb": "mysql+projectdb://{user}:{passwd}@{ip}:3306/projectdb",
    "resultdb":"mysql+resultdb://{user}:{passwd}@{ip}:3306/resultdb",
    "message_queue": "redis://:{passwd}@127.0.0.1:6379/db",
    "webui": {
        "port":5000,
        "username": "{your.name}",
        "password": "{your.passwd}",
        "need-auth": true
    }
}
启动pyspider
pyspider -c pyspider.config.json all

如果报错

libcurl link-time ssl backend (nss) is different from compile-time ssl backend (openssl)
||
# 重装pycurl
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=nss
easy_install pycurl

如果报错

    from werkzeug.wsgi import DispatcherMiddleware
ImportError: cannot import name DispatcherMiddleware
||
# werkzeug换低版本
python -m pip uninstall werkzeug
python -m pip install werkzeug==0.16.0
启动脚本
#!/bin/sh
cd `dirname $0`
if [ `ps -ef | grep 'pyspider' |grep -v 'grep' | wc -l` -lt "1" ];
then
    nohup pyspider -c pyspider.config.json all  &
    echo "pyspider started"
fi

基本使用

self.crawl里面常用的参数

url
需要爬取的url列表
callback
这个参数用来指定爬取内容后需要哪个方法来处理内容.一般解析为 response. default:
age
任务有效期,以秒为单位
priority
爬取优先级,数字越大优先级越大
exetime
爬取任务定时,默认为0,就是即时爬取,
retries
失败重新执行次数,默认3次
itag
任务标记值,此标记会在抓取时对比,如果这个值发生改变,不管有效期有没有到都会重新抓取新内容.
method
HTTP请求,默认为GET
params
URL后面的请求参数,字典
data
POST请求参数
proxy
设置代理服务器
save
传递一个对象给任务,在任务解析时可以通过response.save来获取传递的值

Response

Response.url获取当前url地址
Response.text获取文本
Response.content二进制数据
Response.doc获取pyquery对象
Response.etree获取lxml对象,使用xpath语法
Response.json 获得的是json串,直接用response.json转成python数据
Response.status_code状态码
Response.orig_url原始url地址
Response.headers 响应头
Response.cookies 
Response.err错误信息
Response.time发起请求到获得请求的响应时间
Response.ok 状态,不是200就报错
Response.encoding 获取编码类型
Response.save 传递
Response.js_script_result  获取js执行之后的结果
Response.raise_for_status() 如果状态结果不是200

捕获错误状态码

@catch_status_code_error  
def callback(self, response):
	...

参考文章
https://segmentfault.com/a/1190000021640557?utm_source=tag-newest
https://www.jianshu.com/p/f8ce607f3de0

你可能感兴趣的:(pyspider在Linux的安装和基本使用)