Scrapy+crontab 定时爬取小说更新推送到手机邮件

Scrapy+crontab 定时爬取小说更新推送到手机邮件

本人酷爱追火星引力的《逆天邪神》小说,但经常是俩三天才一更,每天打开浏览器查看是否更新贼痛苦。现在利用所学的知识来解决问题。文章分三部分:一、爬取更新 二、发送邮件 三、定时任务。爬虫内容简单,适合scrapy新手,重点在于发送邮件和定时任务。

一、爬取更新

小说地址:http://m.zongheng.com/h5/book?bookid=408586
页面图片:这里写图片描述
1.创建工程project
选择一个目录:/home/yunge/code/spiders/
执行命令:

scrapy startproject xiaoshuo

2.创建spider
进入工程内,执行命令:

cd /xiaoshuo/xiaoshuo

创建spider,执行命令:

scrapy genspider nitianspider zongheng.com

3.items.py 填空

import scrapy

class XiaoshuoItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    chapter=scrapy.Field()
    updatetime=scrapy.Field()

4.nitiansipder.py

# -*- coding: utf-8 -*-
import scrapy
from xiaoshuo.items import XiaoshuoItem
from scrapy.mail import MailSender

class NitianspiderSpider(scrapy.Spider):
    name = 'nitianspider'
    allowed_domains = ['zongheng.com']
    start_urls = ['http://m.zongheng.com/h5/book?bookid=408586']

    def parse(self, response):
        item=XiaoshuoItem()
        item['chapter']=response.xpath('//span[@class="last_tit"]/text()').extract()[0]
        item['updatetime']=response.xpath('//div[@class="time"]/text()').extract()[0]

        return item

二、发送邮件

5.pipelines.py

from scrapy.mail import MailSender
import scrapy

class XiaoshuoPipeline(object):
    def process_item(self, item, spider):
        with open ('nitianxieshen.txt','r') as fp:
            line=fp.readline().split('\t')
            if item['chapter']!=line[0]:
                Subject=u'小说《逆天邪神》更新啦!!!'
                Body=u'更新内容:\n%s\n点击地址:\nhttp://m.zongheng.com/h5/book?bookid=408586'%item['chapter'
                mailer=MailSender(smtphost="smtp.163.com",mailfrom="*****@163.com",smtpuser="******@163.com",smtppass="******",smtpport=25)
                mailer.send(to=['*******@qq.com'],subject=Subject.encode('utf8'),body=Body.encode('utf8'))
                with open('nitianxieshen.txt','w') as fp:
                    fp.write(item['chapter'].encode('utf8')+'\t')
                    fp.write(item['updatetime'].encode('utf8')+'\t\n')
        return item

6.settings.py
在最后一行添加:

ITEM_PIPELINES={'xiaoshuo.pipelines.XiaoshuoPipeline':2}

7.执行验证
首先创建文件 nitianxieshen.txt :

vim nitianxieshen.txt

随便写入内容,与爬取的内容不同,方便测试爬虫是否发送邮件成功。
执行命令:

scrapy crawl nitianspider

三、定时任务

8.在目录:/home/yunge/code/spider/xiaoshuo/xiaoshuo/ 下,
创建脚本crontab.sh,内容:

cd /home/yunge/code/spiders/xiaoshuo/xiaoshuo/
scrapy crawl nitianspider

9.脚本添加文件执行权限
执行命令:

chmod 774 crontab.sh

10.crontab 添加定时任务
执行命令:

crontab -e

写入:

30 */6 * * * /home/yunge/code/spiders/xiaoshuo/xiaoshuo/crontab.sh

从6:30开始,每隔6个小时执行一次crontab.sh。

11.取消脚本执行后发送电脑邮件
此邮件非爬虫里的邮件,是cron定时任务发送电脑用户(即yunge)的邮件,执行后会在命令端不断提醒。
重新修改:

crontab -e

内容:

30 */6 * * * /home/yunge/code/spiders/xiaoshuo/xiaoshuo/crontab.sh &> /dev/null

到此已经结束了,以后可以美滋滋地看小说了。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值