环境配置
由于 scrapyd是基于python3+以上的版本兼容性较好,所以我们需要的环境为
python3.4+
scrapyd
scrapy & scrapy相关的库
scrapy_reids 如果缺少这个在部署任务会报错 (Reids.KeyErroe:'xxx'), xxx我们部署的任务.
SpiderKeeper `pip install spiderkeeper' 就可以安装.
启动&配置
安装完成之后,便可以启动服务器了.博主本人使用的是ubuntu,所以就以ubuntu为例,win&macos进本一样.
单台服务器
sudo spiderkeeper
# 启动单台服务器,默认启动本地的 http://localhost:6800 scrapyd的服务 | spiderkeeper的默认端口为5000.
连接多台scrapyd服务器.
在分布式中我们的服务器中肯定不止一台,使用spiderkeeper可以很好的解决这个问题
sudo spiderkeeper --server=http://localhost:6800 --server=http://111.111.111.111:6800
#启动一个spiderkeeper可以同时部署两台服务器的spider
更改用户名&密码&更改端口号
config.py 更改用户名&密码
# Statement for enabling the development environment
import os
DEBUG = True
# Define the application directory
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(os.path.abspath('.'), 'SpiderKeeper.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
DATABASE_CONNECT_OPTIONS = {}
# Application threads. A common general assumption is
# using 2 per available processor cores - to handle
# incoming requests using one and performing background
# operations using the other.
THREADS_PER_PAGE = 2
# Enable protection agains *Cross-site Request Forgery (CSRF)*
CSRF_ENABLED = True
# Use a secure, unique and absolutely secret key for
# signing the data.
CSRF_SESSION_KEY = "secret"
# Secret key for signing cookies
SECRET_KEY = "secret"
# log
LOG_LEVEL = 'INFO'
# spider services
SERVER_TYPE = 'scrapyd'
SERVERS = ['http://localhost:6800']
# basic auth 这里更改用户名&密码
NO_AUTH = False
BASIC_AUTH_USERNAME = 'admin'
BASIC_AUTH_PASSWORD = 'admin'
BASIC_AUTH_FORCE = True
run.py更改端口号
def parse_opts(config):
parser = OptionParser(usage="%prog [options]",
description="Admin ui for spider service")
parser.add_option("--host",
help="host, default:0.0.0.0",
dest='host',
default='0.0.0.0')#bind ip 绑定ip 默认全部人可以访问
parser.add_option("--port",
help="port, default:5000",
dest='port',
type="int",
default=5000)#默认端口号5000 可以根据你的需求设计
parser.add_option("--username",
help="basic auth username ,default: %s" % config.get('BASIC_AUTH_USERNAME'),
dest='username',
default=config.get('BASIC_AUTH_USERNAME'))
parser.add_option("--password",
help="basic auth password ,default: %s" % config.get('BASIC_AUTH_PASSWORD'),
dest='password',
default=config.get('BASIC_AUTH_PASSWORD'))
parser.add_option("--type",
help="access spider server type, default: %s" % config.get('SERVER_TYPE'),
dest='server_type',
default=config.get('SERVER_TYPE'))
parser.add_option("--server",
help="servers, default: %s" % config.get('SERVERS'),
dest='servers',
action='append',
default=[])
parser.add_option("--database-url",
help='SpiderKeeper metadata database default: %s' % config.get('SQLALCHEMY_DATABASE_URI'),
dest='database_url',
default=config.get('SQLALCHEMY_DATABASE_URI'))
parser.add_option("--no-auth",
help="disable basic auth",
dest='no_auth',
action='store_true')
parser.add_option("-v", "--verbose",
help="log level",
dest='verbose',
action='store_true')
return parser.parse_args()