Scrapy+crontab 定时爬取小说更新推送到手机邮件
来源:互联网 发布:淘宝seo怎么优化关键词 编辑:程序博客网 时间:2024/06/15 14:17
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 scrapyclass 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 scrapyfrom xiaoshuo.items import XiaoshuoItemfrom scrapy.mail import MailSenderclass 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 MailSenderimport scrapyclass 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 0
- Scrapy+crontab 定时爬取小说更新推送到手机邮件
- scrapy实例 ----- 爬取小说
- 爬虫第五战 scrapy小说爬取
- 实战scrapy-爬取红袖添香前20页小说
- scrapy框架爬虫定时爬取
- 使用Scrapy爬取顶点小说整个网站的小说,入库Mysql!
- 手机没有推送更新系统,如何更新到 iOS 11
- Python使用Scrapy爬虫框架爬取天涯社区小说“大宗师”全文
- python爬虫(16)使用scrapy框架爬取顶点小说网
- Python爬取小说
- 爬取起点小说
- python爬取小说
- requests爬取小说
- scrapy爬取某个手机app的文章数据
- Scrapy爬取网页并保存到数据库中
- 实战 使用scrapy 爬取代理 并保存到数据库
- Scrapy实例1-爬取天气预报存储到Json
- scrapy爬取豆瓣电影top250并存储到mysql
- iOSBLE开发备忘录之系统提示
- cartographer源码分析(26)-io-file_writer.h
- BZoj 1083: [SCOI2005]繁忙的都市( 最小生成树
- 三角网格参数化几种简单的方法比较
- Unity3D之UI按键绑定事件(一)
- Scrapy+crontab 定时爬取小说更新推送到手机邮件
- cartographer源码分析(27)-io-points_batch.h
- 基于Python的开源人脸识别库:离线识别率高达99.38%——新开源的用了一下感受一下
- Java多线程学习笔记
- vb.net 教程 12-1 WebBrowser 3
- Eclipse环境安装Python插件PyDev
- cartographer源码分析(28)-io-points_processor.h
- 古文觀止卷七_原毁_韓愈
- 解决excel导出大数字溢出或者被科学计数法格式化