二.scrapy抓取百度新闻排行榜,并且推送到指定邮箱
来源:互联网 发布:steam mac免费游戏推荐 编辑:程序博客网 时间:2024/05/16 19:27
#encoding=utf-8
import scrapyimport requestsfrom pymongo import MongoClientfrom ..items import FirstoneItemimport smtplibfrom email.mime.text import MIMETextfrom .. import settingsimport timefrom scrapy.http import Requestfrom scrapy.spiders import Spiderclass MaimaiSpider(Spider): #mongodb cn=MongoClient(settings.MONGODB_HOST,settings.MONGODB_PORT) db=cn[settings.MONGODB_DB] tb=db[settings.MONGODB_TABLE] name='baidunews' allowed_domains=['baidu.com'] start_urls=['http://top.baidu.com/buzz?b=341'] mainurl='http://top.baidu.com/' def parse(self, response): modes=response.xpath('//div[@class="hblock"]/ul/li/a/@href').extract() for mode in modes[1:]: news_type=response.xpath('//div[@class="hblock"]/ul/li[{}]/a/@title'.format(str(1+modes.index(mode)))).extract_first() yield Request(url=self.mainurl+mode[1:],callback=self.parse_item,meta={'news_type':news_type}) def parse_item(self,response): bodys=response.xpath('//table[@class="list-table"]/tr') for body in bodys: if body.xpath('.//td[@class="first"]').extract(): items=FirstoneItem() num=body.xpath('.//td[@class="first"]/span/text()').extract_first() title=body.xpath('.//td[@class="keyword"]/a/text()').extract_first() href=body.xpath('.//td[@class="keyword"]/a/@href').extract_first() focus_num=body.xpath('.//td[@class="last"]/span/text()').extract_first() items['num']=num items['_id']=title items['news_type']=response.meta['news_type'] items['baidu_url']=href items['focus_num']=focus_num yield items # print response.meta['news_type'].encode('gb18030'),num,title.encode('gb18030'),href def close(self, reason): if reason=='finished': header='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><table border="0" cellspacing="0" cellpadding="3" align="left" >' tail='</table></body></html>' line='' for data in self.tb.find(): if int(data['num'])<=3: tp0='<tr align="left"><td colspan="6">%s</td></tr>'%('*'*10) tp1='<tr align="left"><td colspan="6">%s</td></tr>'%data['news_type'] tp2='<tr align="left"><td colspan="6">%s</td></tr>'%data['num'] tp3='<tr align="left"><td colspan="6">%s</td></tr>'%data['_id'] tp4='<tr align="left"><td colspan="6">%s</td></tr>'%data['baidu_url'] line=line+tp0+tp1+tp2+tp3+tp4 body=header+line+tail msg = MIMEText(body,'html', 'utf-8') msg["Subject"] = "[%s]BaiduTopNews"%time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) msg["From"] = settings.email_From msg["To"] = ','.join(settings.email_To) try: s = smtplib.SMTP_SSL(settings.smtpHost, settings.smtpPort) s.login(settings.email_From,settings.email_pwd) s.sendmail(settings.email_From, msg["To"], msg.as_string()) s.quit() print "Success!" except smtplib.SMTPException,e: print "sendemail_Falied,the reson is %s"%e
阅读全文
2 0
- 二.scrapy抓取百度新闻排行榜,并且推送到指定邮箱
- Python爬虫抓取最新段子发送到指定邮箱
- 点击百度推送通知,跳转到指定Activity
- 百度消息推送点击跳转到指定界面的实现
- scrapy抓取 W3School (二)
- 百度云推送点击推送通知后跳转到指定activity
- 百度贴吧邮箱自动抓取,QQ号自动抓取,论坛邮箱QQ号自动抓取
- 使用scrapy进行大规模抓取(二)
- 使用scrapy进行大规模抓取(二)
- scrapy(二)之模拟登陆实现抓取
- 如何实现程序调用系统邮件,发送app中抓取的xml图片和内容到指定的邮箱
- 推送跳转到指定页面
- 百度推送始终跳转不到指定页面
- 百度贴吧和论坛的邮箱自动抓取程序
- PHP抓取百度贴吧邮箱v1.0
- Scrapy爬取百度图片(二)
- iOS推送详解(二)收到通知跳到指定页面的处理办法
- 静态页面,表单发送到指定邮箱
- <NOIP> 5 . P1422 小玉家的电费
- Hadoop 2.7.0在centos7分布式集群上的部署与测试
- location
- org.springframework.beans.factory.BeanCreationException---这个错误怎么解决,求助!!!
- JavaWeb:MySql基本操作
- 二.scrapy抓取百度新闻排行榜,并且推送到指定邮箱
- 同步与异步IO区别
- ReentrantLock可重入锁简介
- java CAS 和ABA问题
- ros中创建msg和srv文件时,配置CMakeLists.txt文件问题
- android多点触控,图片的拖拽与放大缩小
- opencv函数之-addWeighted
- 三五、Git上传代码到码云(上传、下载、修改账号、生成SSH公钥)
- 如何将bug杀死在摇篮里?