利用爬虫、SMTP和树莓派3B发送邮件(爬取墨迹天气预报信息)

-----------------------------------------学无止境-----------------------------------------

前言:大家好,欢迎来到誉雪飞舞的博客园,我的每篇文章都是自己用心编写,

算不上精心但是足够用心分享我的自学知识,希望大家能够指正我,互相学习成长。

 

转载请注明:https://www.cnblogs.com/wyl-pi/p/10635476.html

 

墨迹天气,不是老年人大都耳熟能详吧,用着呢吧或者或用过吧,嗯...的确不错。

(过一阵子再给大家更新一个带语音播报的电子邮箱之类的东西出来,分享给大家,敬请期待吧。)

 

 会运行错误,得先安装库 sudo pip install SMTP or sudo apt-get SMTP

尴尬我也忘了.....

先上一个百度的非常实用简单易懂的小实例,作为下文的学习基石。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
# 第三方 SMTP 服务
mail_host="smtp.sohu.com"  #设置服务器
mail_user="wyl1346****525@sohu.com"    #用户名
mail_pass="**密码-password**"   #口令  

sender = 'wyl1346*****25@sohu.com'
##搜狐向QQ发送邮件
receivers = ['1346*****25@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

##邮件的来去地址
message = MIMEText('Here is email_text.Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] =  Header(receivers[0], 'utf-8')

##  邮件内容    经过Header函数编译成utf-8格式的邮件  ##
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
 
try:
    SmtpObj = smtplib.SMTP()  # 创建 SMTP 对象
    print("SMTP complete")

    SmtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
    print("connect complete")        # mail_host服务器 地址

    SmtpObj.login(mail_user,mail_pass)  
    print("login complete")

##  SMTP.sendmail(from_addr, to_addrs, msg)  ##
##  from发送者地址   to接受者地址    msg发送的内容
    SmtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")

except smtplib.SMTPException:
    print("Error: 无法发送邮件")

 

以后有我这个文章你手机可以删掉某APP,缓解内存压力了。而且每天通过邮件查看天气非常个性,

高大上说不上起码是不走寻常路的奇葩,哈哈,没错我就是这样。

毕竟好钻研不是坏事,不要总是拿一些唾手可得的东西,没啥意思,没有技术含量,自己动手,嗯,是吧!那感觉就是不一样。

 

好,上面的小实例看懂后,后面也好办,no problem.

  1 import time
  2 import smtplib
  3 import requests
  4 import random
  5 import socket
  6 import bs4
  7 from bs4 import BeautifulSoup
  8 from email.mime.text import MIMEText
  9 from email.header import Header
 10 
 11 def get_content(url):
 12         header = {
 13         'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
 14         'Accept-Encoding' : 'gzip, deflate',
 15         'Accept-Language' : 'zh-CN,zh;q=0.9,en;q=0.8',
 16         'Cache-Control' : 'max-age=0',
 17     'Connection' : 'keep-alive',
 18         'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
 19         }
 20         timeout = random.choice(range(80, 180))
 21         while True:
 22                 try:
 23                         rep = requests.get(url,headers = header,timeout = timeout)
 24                         rep.encoding = 'utf-8'
 25                         break
 26                 except socket.timeout as e:
 27                         print( '3:', e)
 28                         time.sleep(random.choice(range(8,15)))
 29                 except socket.error as e:
 30                         print( '4:', e)
 31                         time.sleep(random.choice(range(20, 60)))
 32         return rep.text
 33 
 34 def get_weather(url,data):
 35         air_list = []
 36         weather_list = []
 37         #weather_status = []
 38         soup = BeautifulSoup(data,'lxml')
 39         div = soup.find('div',{'class' : 'forecast clearfix'})
 40 
 41         air_quality = div.find('strong',class_='level_2').string    #空气质量
 42         date = div.find('a',href='https://tianqi.moji.com/today/china/shandong/penglai').string
 43         wind_direction = div.find('em').string   #风向
 44         wind_grade = div.find('b').string           #风速
 45         ul = div.find('ul',{'class' : 'clearfix'})
 46 
 47 ##  天气情况抽取  ##
 48         a = []
 49         li = ul.find_all('li')
 50         j=0
 51         #return li
 52         for i in li:
 53             j+=1
 54             if j==2:
 55                 a = i
 56                 a = str(a).replace('
','').replace('	','').replace(' ','').replace("</li>","").replace('
','')
 57                 a = a.replace('<li><span>','').replace('<imgalt=','').replace('src="https://h5tq.moji.com/tianqi/assets/images/weather/','')
 58                 a = a.replace('.png/></span>','').replace('.png"/></span>','').replace('"','').replace('	','')
 59                 
 60                 for x in range(100,0,-1):
 61                     #print("w{0}".format(x))
 62                     a = a.replace(("w{0}".format(x)),'')
 63 
 64         if(len(a)==2):
 65             a = a[0:1]
 66         if(len(a)==4):
 67             a = a[0:2]
 68 
 69         for day in li:
 70             if not isinstance(day,bs4.element.Tag): 
 71                 date = day.find('a',href='https://tianqi.moji.com/today/china/shandong/penglai').string
 72 
 73             weather_list.append(day.string)
 74             if not isinstance(day,bs4.element.Tag):
 75                 wind_direction = day.find('em').string
 76                 wind_grade = day.find('b').string
 77                     
 78         Tempreture = weather_list[2]
 79         air_quality =air_quality.replace("
","").replace(' ','')
 80         #air_quality = str(air_quality).replace('
','').replace(' ','')
 81         #print("'data' {0} 'Tempreture' {1},air_quality {2}
,'wind_grade' {3},'wind_direction' {4}".format(date,Tempreture,air_quality,wind_grade,wind_direction))
 82         return (" 时   间 : {}
 天气情况: {}
 温   度 : {}
 风   向 : {}
 风   速 : {}
 空气质量: {}
".format(date,a,Tempreture,wind_direction,wind_grade,air_quality))
 83                        
 84 def send_email(email):
 85         '''
 86         sender = input('From: ')
 87         password = input('password: ')
 88         smtp_server = input('SMTP_Server: ')
 89 '''
 90 
 91         ##     FROM    ##
 92         sender = 'wyl1346788525@sohu.com'
 93         sent_host = 'smtp.sohu.com'
 94         sent_user = 'wyl1346788525@sohu.com'
 95         sent_pass = 'cxlgWYL1'
 96 
 97         ##     TO     ##
 98         receivers = ['24******09@qq.com'] 99 
100         #message = MIMEText("亲爱的,我现在来播报今天的蓬莱天气。
(嗯...其实现在还只能看不能播)如下所示:
 时 间| 天气情况 | 温 度 | 风 向 | 风 速 | 空气质量
'{0}
".format(result),'plain','utf-8')
101         message = MIMEText("亲爱的今天蓬莱天气是这样的呦 :
{}
".format(result),'plain','utf-8')
102         
103         ##   JUST USE TO DISPLAY   ##
104         message['From'] = Header('自定义From','utf-8')
105         message['To'] = Header('自定义To','utf-8')
106         Subject = "今天的天气预报详情!"
107         message['Subject'] = Header(Subject,'utf-8')   #标题
108         try:
109             server = smtplib.SMTP()
110             #server = smtplib.SMTP(sent_host,25)
111             print("SMTP complete")
112 
113             server.connect(sent_host,25)
114             print("connect complete")
115             
116             #server.set_debuglevel(1)
117             server.login(sent_user,sent_pass)
118             print("login complete")
119 
120             server.sendmail(sender,receivers[0],message.as_string())
121             print("邮件发送成功")
122             #server.quit()
123 
124         except smtplib.SMTPException:
125             print("Error:发生未知错误,无法发送邮件!")
126 
127 if __name__  == '__main__':
128         result =[]
129         url = 'http://tianqi.moji.com/weather/china/shandong/penglai'
130         data = get_content(url)
131         result = get_weather(url,data)
132         result = str(result).replace("\r","").replace('	','').replace('
','').replace("\n","")
133         print("result is 
{}
".format(result))
134         send_email(result)

注:这篇文章我是在win10系统下的演示,我昨天试了一下在树莓派图形化界面下不太一样具体实验操作,

在我的下一篇博客里,下文有链接,只是需要安装一下库SMTP,小改动根据提示来就行,好吧我的错,

我抽出空来再把在树莓派下的测试结果分享给大家,然后写完.py 文件的python脚本,在.sh下编辑生成

个开机自启的脚本就每天都能发送今天的天气预报到你的邮箱里,received设置接受人群,不知可以

给你发送可以给你的爱人,你的兄弟姐妹,你的爸爸妈妈,没错,送上一份你的爱,传递温暖。

嗯中间有很多小BUG我都修复了,但是这个运行结果还是有问题,细心的朋友应该发现了,看到了吧,天气情况:晴w

我们先找到天气情况的变量名为 a,所以找到对于变量 a 的编辑的地方查询问题。

先print(a)看为什么 天气情况 剪裁后的信息,输出变量 a 为:"晴w0晴",哦.....经过我的查询,发现是因为

for x in range(100,1,-1)的原因(这张截图是我改动之后的)它只遍历到w1,没有到w0,,,粗心了,很多朋

友可能会问我为什么加这些多此一举的for直接去掉w0不就行了,我第一天也是这么想的,然而第二天它就变

成了w3、w1、w10、、、、,所以我就简单粗暴的直接for遍历;后面的 len 函数是因为每天的天气状况不同

比如"小雨","晴","阴"字数上有不同我通过 len 函数进行处理截取我想要留下的内容,呈现给用户;

(因为初期bug挺多的,而且使用BeautifulSoup库里有个挺废头的问题,过了挺久了,忘了,反正差不多是

型的问题,网上能百度出来很多,但是出问题的解析答案寥寥无几,几乎没有,也只能翻墙出去还有不少,

但英语硬伤的,翻译很多原意就曲解了,致使本来就懵,看完之后可想而知的状态.......)我后来呢是另辟新

径找到了别的解决方法,所以没有继续太过纠结这个问题,我推荐大家还是耐住性子钻研下去,的确成长,

不过也要把握个度(我是因为要比赛赶进度时间不多,不允许我钻下去),不要非得不撞南墙不回头,不整出来誓不罢休,精神可嘉但效果欠佳。

  • Tag
  • NavigableString
  • BeautifulSoup
  • Comment

如有网友大佬有推荐切实可行的优化方法,我一定洗耳恭听,我在这提前谢谢您了。

 

修复好bug运行检测:

 

检验邮箱结果:

更新的文章,在树莓派图形化界面的本文操作实验,在我的下一篇博客里。

https://www.cnblogs.com/wyl-pi/p/10637688.html

过一阵子再给大家更新一个带语音播报的邮件出来?我也不知道没想好,会分享给大家,敬请期待吧。

当然这篇教程只是用来学习,请勿进行商业活动甚至非法活动,切勿侵害他人权益,提前声明概不负责。

 

如果觉得我的文章还不错,关注一下,顶一下 ,我将会用心去创作更好的文章,敬请期待。

原文地址:https://www.cnblogs.com/wyl-pi/p/10635476.html