简易实现关注微博博主新动态,自动邮件通知

 

处理数据用到了正则+Xpath,发信使用yagmail

实现功能:关注微博博主动态,有新动态自动发邮件提醒,无需登录微博账号

限制:非登录状态只能获取大V前4条微博,不能获取个人用户微博内容

以@英式没品笑话百科https://weibo.com/sickipedia 为例

 1 # coding=utf-8
 2 from lxml import etree
 3 import re
 4 import requests
 5 import yagmail
 6 import base64
 7 import time
 8 # import sys
 9 # reload(sys)
10 # sys.setdefaultencoding("utf-8")
11 
12 # ff = open('weibo.txt', 'r')
13 # html = ff.read()
14 # ff.close()
15 
16 email = 'xxx@qq.com'#发信邮箱
17 pwd = 'base64(password)'#授权码的base64 不是密码
18 top = 'M_FnveA9gYB'#置顶微博div内的id=""
19 latest = 'M_GtwZB8SH1'#最新微博div内的id=""
20 print '[*]Task start \t\t@ ' + time.asctime()
21 sendSmpt = yagmail.SMTP(user=email, password=base64.b64decode(pwd), host='smtp.qq.com')#发信设置,需要去邮箱开启smtp
22 sendSmpt.send(to=email, subject="[python-sina]Start working", contents='Rt')
23 
24 while True:
25     html = requests.get('https://weibo.cn/sickipedia').content
26     selector = etree.HTML(html)
27     title = selector.xpath('/html/head/title/text()')
28     data = selector.xpath('//div[starts-with(@id,"M_")]')
29     _id = re.findall('class="c" id="(.*?)"', html, re.S)
30     if _id[0] != top:#检查置顶微博是否变化
31         print '[*]Top has changed. \t@ ' + time.asctime()
32         top = _id[0]
33 
34     if _id[1] != latest:#检查最新一条微博
35         print '[+]new weibo find \t@ ' + time.asctime()
36         content = data[1].xpath('string(.)')
37         text = [content]
38         file_text = bytes(bytearray(content, encoding='utf-8'))
39         sendSmpt.send(to=email, subject=u"[python-sina]你关注的博主更新啦", contents=text)#推送新动态至邮箱
40         print '[+]Email send success'
41         f = open('weibo_new.txt', 'a')
42         now_time = time.asctime()
43         f.write(now_time + '\n' + file_text + '\n\n')
44         f.close()
45         latest = _id[1]
46     time.sleep(180)#刷新间隔时间180秒
47 #错误通知
48 error_text = ['check python status.']
49 sendSmpt.send(to=email, subject="[python-sina]Status error", contents=error_text)
50 print '[!]task halt \t\t@ ' + time.asctime()

收信效果图: 

 

转载于:https://www.cnblogs.com/zzwanqi/p/9437387.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部