本项目fork自ScrapydArt: https://github.com/dequinns/ScrapydArt
$ bash Anaconda3-2019.03-Linux-x86_64.sh
$ pip install -r req.txt
...
database_type = sqlite
...
...
mysql_host = 127.0.0.1
mysql_port = 3306
mysql_user = root
mysql_password = mysql
mysql_db = scrapydartTest
...
...
clear_up_database_when_start = yes // yes/no, 每次运行前清理观察数据
observation_times = 20 // >0的整数,设置异常监控的观察次数
strict_mode = no // yes/no,设置是否严格模式
strict_degree = 4 // >0的任意数,设置严格模式的严格程度,越低越严格
...
在程序正确安装后,每次运行时都会有配置文件修改提示(包含以上部分配置选项),您可以手动修改这些配置,所以不用担心配置文件的位置问题,也不用担心程序配置错误了,直接重启程序再配置就行了,程序会保留您最后一次修改的信息。
$ curl http://localhost:6800/scheduletodb.json -d project=MyProject -d spider=my_spider -d schedule='{"year":"2019", "month": "08", "day": "28", "hour": "09", "minute": "19", "second": "00"}' -d spider_args='{}' -d status='1'
import requests
post_data = {
'project': 'my_project', # str
'spider': 'my_sdpier', # str
'schedule': '{"day": "*/1", "hour": "19"}', # str dict
'spider_args': '{"times": 1, "method": "auto_increment"}', # str dict
'status': 1, # int
}
result = requests.post(url='http://localhost:6800/scheduletodb.json', data=post_data)
spider_args 支持动态变量模式, 需要指定其内部的 "method" 字段, 如果不给定 "method", 或者 "method" 的值为"normal", 则每次自动调度都会使用同样的参数;
如果指定 "method" 为 "auto_increment", 则args参数中是数值类型的参数会在每次调度时自动增加整数1, 其他非数值类型参数不变, 而且 "auto_increment" 方法仅支持整数变化;
- "method" 的值设置为一个字典, 字典的键为 "expression" 或者 "function", 意思是表达式或者方法, 在调度器内部, "expression"会被执行 eval() 方法, "function" 会被执行 exec()方法
- "expression" 或者 "function" 的值设置为表达式或者方法的字符串, 例如:
"method": { "expression": "{k: v+1 for k, v in args.items()}" }
- 表达式生成的值会被调度器内的变量 "next_args" 所接收, 用于下一次调度的传参
- "function" 的值会直接用exec()运行, 所以 "function" 的值的运行结果需要对变量 "next_args" 作出改变, 例如:
"method": { "function":
"def func(dic):
next_dic = {}
for k, v in dic.items():
next_dic[k] = v + 1
return next_dic
next_args = func(args)" }
- 即可以将参数变量"args"通过你自己编写的方法变为下一次的变量"next_args"
- 注意, 参数 "args" 和下次参数 "next_args" 是的名称在程序内部是固定的, 不可更改否则不起效果, 甚至报错
- 0 : 不启用此条调度
- 1 : 正常启用
- 2 : 高级模式
- 3 : 最高级模式
{"second": "*/30"}
...
'schedule': {
'year': 2019,
'month': 6,
'day': 4,
'hour': 10,
'minute': 50,
'second': 50
}
(2019-06-04 10:50:50)
"schedule": {"hour": 10}
{'week': '*/2'}
'2019,9,13,16,30,0' # 2019年9月13日16点30分00秒
{"node_name": "name of node", "status": "ok"}
[- Scheduler -#info] job default-xinhua is waiting, countdown 540s
↑ ↑ ↑
项目名 爬虫名 距下次运行秒数
$ curl http://localhost:6800/listdbschedule.json -d projects="['project_name1', 'project_name2']" -d spiders="['spider1', 'spider2']"
import requests
url = 'http://127.0.0.1:6800/listdbschedule.json?un=&pwd='
data = {
'projects': str(['project1', 'project2']),
'spiders': str(['spider1', 'spider2']),
}
res = json.loads(requests.post(url=url, data=data).content)
print(res)
{"node_name": "name of node", "status": "ok", "database_schedules":
[{'id': schedule_id,
'project': project_name,
'spider': spider_name,
'schedule': spider_schedule,
'args': spider_args,
'status': spider_status
},
{... ...},
...
] }
$ curl http://localhost:6800/updatedbschedule.json -d id=update_id -d schedule=new_schedule -d args=new_args -d status=new_status
import requests
post_data = {
'id': update_id,
'schedule': new_schedule,
'args': new_args,
'status': new_status,
}
result = requests.post(url='http://localhost:6800/updatedbschedule.json', data=post_data)
{"node_name": "name of node", 'update': "ok"}
$ curl http://localhost:6800/rmschedulefromdb.json -d id=delete_id
import requests
post_data = {
'id': delete_id,
}
result = requests.post(url='http://localhost:6800/rmschedulefromdb.json', data=post_data)
{"node_name": "name of node", 'delete': "ok"}
若在配置中设置了auth_username 和 auth_password,则需要在每次post请求的data中加入 {'un': user_name, 'pwd': password}, get 请求中加入 ?un=user_name&pwd=password。
...
Terminator = deactivate
...
此功能会每隔数秒巡视一遍所有正在运行的爬虫,并根据之前的运行时间计算方差数与均值,推算判断本次运行的时间是否合理,若不合理,则会在几秒钟内终结此任务, 不需要担心爬虫任务永远被杀死了,除非你设置的是单次定时任务,否则爬虫将在下一个周期继续正常启动, 若设置为 Terminator 设置为 "activate",将会在平台启动时在后台显示日志:
[- TERMINATOR -#info] Terminator Started
[- TERMINATOR -#warn] Scan completed
[- TERMINATOR -#warn] Scan completed, Terminated target: '[default-xinhua-6796090a940b11e98d6854e1adc0a997, default-fifa-6796090a940b11e98d6854e1adc0a997]'
[project-spider-jobid, ...]