以前
定闹钟的意义是每天叫我起床
现在
我觉得
是该报达他的时候了
于是我
每天叫闹钟起床
众所周知
早上的时光很慢
眼睛一闭一睁
才过了五分钟
早上的时光很快
眼镜一闭一睁
就迟到五分钟
为了在有限的时间里做更多的事情
那就得把起床前必做的事情
列成清单
《情侣间必做的一百件事》
啊呸
《起床前必做的一百件事》
分析:
1、每天定时发送 当日的天气、热搜、以及其他你所关心的事情
2、发送格式为邮件发送
翠花,上酸菜:
1、抓取微博热搜信息
def GetWeiboRealtimeHotInfos(maxCount=10) :
headUrl = "https://s.weibo.com/"
hotUrl = "https://s.weibo.com/top/summary?cate=realtimehot"
res = requests.get(hotUrl)
res.raise_for_status()
#print(res.text)
res.encoding = res.apparent_encoding
strInfo = "\n今日微博热搜:\n"
pattern = '<a href=".*?" target=".*?">.*?</a>'
re_result = re.findall(pattern, res.text)
for i, result in enumerate(re_result) :
pattern = '>(.*?)<'
result_text = re.findall(pattern, result)
if len(result_text) > 0 :
strInfo = strInfo + result_text[0] + "\n"
#print(result_text)
pattern = '<a href="(.*?)"'
result_url = re.findall(pattern, result)
if len(result_url) > 0 :
strInfo = strInfo + headUrl + result_url[0] + "\n"
#print(headUrl + result_url[0])
if i >= maxCount-1 :
break
return strInfo
注:此处只有代码,详细解释可以参考前一篇文章,内有详细信息以及代码含义解释:
《Python-爬虫教程(抓取微博热搜、附源码)》
可以关注微信公众号查看内容:
?
2、抓取当日天气信息。此处和抓取微博热搜不同,微博热搜抓取的数据格式使用正则表达式筛选,这里仅需要json化就可以
def GetWeatherInfo(cityName) :
weatherJsonUrl = WEATHER_URL + cityName
response = requests.get(weatherJsonUrl) # 获取并下载页面,其内容会保存在respons.text里
response.raise_for_status() # 请求失败的话就会抛出异常
# 将json文件格式导入成python的格式
weatherData = json.loads(response.text)
#print(weatherData)
weather_dict = dict()
weather_dict['type'] = weatherData['data']['forecast'][0]['type']
weather_dict['high'] = weatherData['data']['forecast'][0]['high']
weather_dict['low'] = weatherData['data']['forecast'][0]['low']
weather_dict['fengxiang'] = weatherData['data']['forecast'][0]['fengxiang']
weather_dict['ganmao'] = weatherData['data']['ganmao']
logInfo = cityName + ": "
for i, value in enumerate(weather_dict.values()) :
logInfo += value
if i < len(weather_dict.values())-1 :
logInfo += ", "
else :
logInfo += "\n"
#print(logInfo)
return logInfo
3、像我这么传统的人,必须关注一下老黄历。
def GetHuangliInfo() :
# 抓取黄历
res = requests.get(HUANGLI_URL)
res.raise_for_status()
#print(res.text)
res.encoding = res.apparent_encoding
# 正则表达式筛选图片下级地址
pattern = '<p>(.*?)</p>'
re_result = re.findall(pattern, res.text)
#print(re_result)
strInfo = ""
for i, result in enumerate(re_result) :
if i == 0 :
#print("\n老黄历:")
strInfo = "\n老黄历:\n"
result = result.replace('<span>', '')
result = result.replace('</span>', '')
result = result.replace('<strong>', '')
result = result.replace('</strong>', '')
result = result.replace('<span class="yj2y">', '')
result = result.replace('<span class="sc2title">', '')
#print(" >>>", result)
strInfo += (" >>>" + result + "\n")
if i >= 9 :
break
return strInfo
4、定时发送。反正此脚本只做一件事,那就是每天给我发送邮件,所以定时操作可以简单点来,直接sleep到对应时间即可。
def GetSubSendMailTime(lastSendMailTime) :
# 定时 7:30发送
nowTime = time.time()
if 0 == lastSendMailTime :
localTime = time.localtime(nowTime)
if localTime.tm_hour >= REMIND_TIME_HOUR :
localTime = time.localtime(nowTime + (24-REMIND_TIME_HOUR+1)*3600)
timeArray = "%d-%d-%d %d:%d:%d" % (localTime.tm_year, localTime.tm_mon, localTime.tm_mday, REMIND_TIME_HOUR, REMIND_TIME_MIN, 0)
timeObj = time.strptime(timeArray, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeObj))
return int(timeStamp - nowTime)
else :
localTime = time.localtime(nowTime + 86400)
timeArray = "%d-%d-%d %d:%d:%d" % (localTime.tm_year, localTime.tm_mon, localTime.tm_mday, REMIND_TIME_HOUR, REMIND_TIME_MIN, 0)
timeObj = time.strptime(timeArray, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeObj))
return int(timeStamp - nowTime)
然后,执行脚本。因为此脚本需要在后台一直运行,所以直接放到永不关机的开发机上,注意,执行命令时需要使用 nohup,防止连接关闭,进程被干掉。
eg:?nohup python weather.py &
执行效果(源码在末尾!!!):
源码:
# -*- encoding:utf8 -*-
import json
import requests
import re
import smtplib
from email.mime.text import MIMEText # 文本
from email.header import Header
import time
import datetime
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
WEATHER_URL = "http://wthrcdn.etouch.cn/weather_mini?city="
HUANGLI_URL = "https://www.huangli8.com"
MAIL_HOST = "smtp.163.com" # 指定SMTP服务器
MAIL_SENDER = "zhanglu_1024@163.com" # 发件人
MAIL_PASS = "ZSKV" # 授权码 并非密码,具体授权码开启方式百度,非常简单
MAIL_RECEIVERS = ['1044878451@qq.com'] # 收件人们
REMIND_TIME_HOUR = 7
REMIND_TIME_MIN = 50
def GetWeatherInfo(cityName) :
weatherJsonUrl = WEATHER_URL + cityName
response = requests.get(weatherJsonUrl) # 获取并下载页面,其内容会保存在respons.text里
response.raise_for_status() # 请求失败的话就会抛出异常
# 将json文件格式导入成python的格式
weatherData = json.loads(response.text)
#print(weatherData)
weather_dict = dict()
weather_dict['type'] = weatherData['data']['forecast'][0]['type']
weather_dict['high'] = weatherData['data']['forecast'][0]['high']
weather_dict['low'] = weatherData['data']['forecast'][0]['low']
weather_dict['fengxiang'] = weatherData['data']['forecast'][0]['fengxiang']
weather_dict['ganmao'] = weatherData['data']['ganmao']
logInfo = cityName + ": "
for i, value in enumerate(weather_dict.values()) :
logInfo += value
if i < len(weather_dict.values())-1 :
logInfo += ", "
else :
logInfo += "\n"
#print(logInfo)
return logInfo
def GetHuangliInfo() :
# 抓取黄历
res = requests.get(HUANGLI_URL)
res.raise_for_status()
#print(res.text)
res.encoding = res.apparent_encoding
# 正则表达式筛选图片下级地址
pattern = '<p>(.*?)</p>'
re_result = re.findall(pattern, res.text)
#print(re_result)
strInfo = ""
for i, result in enumerate(re_result) :
if i == 0 :
#print("\n老黄历:")
strInfo = "\n老黄历:\n"
result = result.replace('<span>', '')
result = result.replace('</span>', '')
result = result.replace('<strong>', '')
result = result.replace('</strong>', '')
result = result.replace('<span class="yj2y">', '')
result = result.replace('<span class="sc2title">', '')
#print(" >>>", result)
strInfo += (" >>>" + result + "\n")
if i >= 9 :
break
return strInfo
def SendMail(strInfo) :
if len(MAIL_RECEIVERS) == 0 :
return
message = MIMEText(strInfo, 'plain', 'utf-8') # 参数分别为文本,格式,编码
message['From'] = MAIL_SENDER
message['To'] = MAIL_RECEIVERS[0]
subject = 'Python Mail ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
message['Subject'] = Header(subject, 'utf-8')
try :
smtpObj = smtplib.SMTP()
smtpObj.connect(MAIL_HOST, 25)
smtpObj.login(MAIL_SENDER, MAIL_PASS)
smtpObj.sendmail(MAIL_SENDER, MAIL_RECEIVERS, message.as_string())
print('send success')
except smtplib.SMTPException :
print('send error')
def GetWeiboRealtimeHotInfos(maxCount=10) :
headUrl = "https://s.weibo.com/"
hotUrl = "https://s.weibo.com/top/summary?cate=realtimehot"
res = requests.get(hotUrl)
res.raise_for_status()
#print(res.text)
res.encoding = res.apparent_encoding
strInfo = "\n今日微博热搜:\n"
pattern = '<a href=".*?" target=".*?">.*?</a>'
re_result = re.findall(pattern, res.text)
for i, result in enumerate(re_result) :
pattern = '>(.*?)<'
result_text = re.findall(pattern, result)
if len(result_text) > 0 :
strInfo = strInfo + result_text[0] + "\n"
#print(result_text)
pattern = '<a href="(.*?)"'
result_url = re.findall(pattern, result)
if len(result_url) > 0 :
strInfo = strInfo + headUrl + result_url[0] + "\n"
#print(headUrl + result_url[0])
if i >= maxCount-1 :
break
return strInfo
def GetSubSendMailTime(lastSendMailTime) :
# 定时 7:30发送
nowTime = time.time()
if 0 == lastSendMailTime :
localTime = time.localtime(nowTime)
if localTime.tm_hour >= REMIND_TIME_HOUR :
localTime = time.localtime(nowTime + (24-REMIND_TIME_HOUR+1)*3600)
timeArray = "%d-%d-%d %d:%d:%d" % (localTime.tm_year, localTime.tm_mon, localTime.tm_mday, REMIND_TIME_HOUR, REMIND_TIME_MIN, 0)
timeObj = time.strptime(timeArray, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeObj))
return int(timeStamp - nowTime)
else :
localTime = time.localtime(nowTime + 86400)
timeArray = "%d-%d-%d %d:%d:%d" % (localTime.tm_year, localTime.tm_mon, localTime.tm_mday, REMIND_TIME_HOUR, REMIND_TIME_MIN, 0)
timeObj = time.strptime(timeArray, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeObj))
return int(timeStamp - nowTime)
def main() :
lastSendMailTime = 0
while(False) :
subSendMailTime = GetSubSendMailTime(lastSendMailTime)
lastSendMailTime = time.time()
print('==> sleep begin...', subSendMailTime)
time.sleep(subSendMailTime)
strInfo = GetWeatherInfo("西安")
strInfo += GetWeatherInfo("伊春")
strInfo += GetWeiboRealtimeHotInfos()
strInfo += GetHuangliInfo()
SendMail(strInfo)
print("==> send today(%d) info success" %lastSendMailTime)
#print(GetSubSendMailTime(lastSendMailTime))
#print(GetWeiboRealtimeHotInfos())
main()
?
呼~
关爱闹钟
人人有责
|