当我们利用Python scrapy框架写完脚本后,脚本已经可以稳定的进行数据的爬取,但是每次需要手动的执行,太麻烦,如果能自动运行,在自动关闭那就好了,经过小编研究,完全是可以实现的,今天小编介绍2种方案来解决这个问题
由于scrapy框架本身没有提供这样的功能,所以小编采用了linux 中crontab的方式进行定时任务的爬取
方案一:
编写shell脚本文件cron.sh
#! /bin/bash
export PATH=$PATH:/usr/local/bin
cd/home/python3/scrapydemo/Ak17/AK17/spiders
nohup scrapy crawl novel>> novel.log 2>&1 &
终端执行命令crontab -e,规定crontab要执行的命令和要执行的时间频率,这里我需要每5分钟就执行scrapy crawl novel 这条爬取命令:
#daemon's notion of time and timezones.#
#Output of the crontab jobs (including errors) is sent through#email to the user the crontab file belongs to (unless redirected).#
#For example, you can run a backup of all your user accounts#at 5 a.m every week with:#0 5 * * 1 tar -zcf /var/backups/home.tgz /home/#
#For more in