当我们写好抓取数据的脚本.py文件时,需要输入命令:scrapy crawl projectName 来执行。
那么怎么做一个定时器,让这个.py脚本进行定时执行呢?
步骤如下:
1、写一个shell脚本,加入命名为test.sh内容如下:
#! /bin/sh
export PATH=$PATH:/usr/local/bin
#进入.py脚本所在目录
cd /data/apps/scrapy/weather/weather/spiders
#执行.py中定义的项目example,并指定日志文件,其中nohup....&表示可以在后台执行,不会因为关闭终端而导致程序执行中断。
nohup scrapy crawl example >> example.log 2>&1 &
2、通过crontab -e命令新增一个定时器,定时执行上面的shell脚本test.sh:
05 8,12,18 * * * (/bin/sh /data/apps/scrapy/weather/weather/spiders/test.sh)