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


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

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

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

     

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

     

    先给大家道个歉,我的上一篇文章中没有在树莓派的交互界面进行操作实验,而且我答应大家一定会把这个实验补上,虽然没太有空整理,

    但是自己说出来的承诺,我必须要兑现,认真对待我的每一篇文章,认真对待我的每个读者,认真对待自己的内心,不能应付着就过去了,

    所以我特意给挤出时间怀着愧疚与坚定地心情再次分享完善这个小项目。

     

     

     一开始出了很多问题,果然还是那句金句名言,切勿眼高手低

    既然答应给大家一个交代,那咱们一个个慢慢说:

    1、上来直接运行有错误的说缺乏bs4、beautifulsoup库,命令安装;

    #python2:
    sudo apt-get install python-bs4 sudo pip install beautifulsoup4
    #python3:
    sudo apt-get install python3-bs4
    sudo pip3 install beautifulsoup4

    关于报错信息:pip3无效命令的解决

      首先安装setuptools(一定要用sudo管理员权限做,如果长时间连接不上主机Ctrl+C,重新执行命令)

    cd /usr/local/src/
    sudo wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz
    tar -zxvf setuptools-19.6.tar.gz
    cd setuptools-19.6/
    python3 setup.py build
    python3 setup.py install

      其次安装pip3(网络不佳很正常,多试几次多试几次)

    cd /usr/local/src/
    sudo wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz
    tar -zxvf pip-8.0.2.tar.gz
    cd pip-8.0.2/
    python3 setup.py build
    python3 setup.py install

    https://www.jianshu.com/p/d4c1a08b993f

    测试安装成功,运行命令pip3成功

    2、报错信息:

    FeatureNotFound: Couldn't find a tree builder with the features you requeste
    
    https://blog.csdn.net/qq_16546829/article/details/79405605

    在报错代码中把函数参数中所有的"lxml"改成"html.parser"

    #例子:
    bs = BeautifulSoup(r, 'lxml').find(.... #改成 bs = BeautifulSoup(r, 'html.parser').find(....
    html.parser是调用python解析器,但是没有解决lxml库不能用的问题啊

    3、看到差别了吧,使用win10_url无法打开网页,你可以试试,所以问题就很严重了,我们根本打不开网页,

    所以后续的爬虫工作根本从源头就已经宣告over了.....这也是一个小细节。

     

    4、中文编码报错信息与解决方案:

    这个报错信息很显然就是一贯的中文编码问题,让人很头疼,我加上了:

    #!/home/pi/SmartHome/SMTP/ok'
    #*_*coding:utf-8_*_

    但依然没卵用,随即我百度找到一位网友写的文章如下:

    https://blog.csdn.net/weixin_39221360/article/details/79525341

    注意如果你的设备同时安装了 Python 2.x 和 Python 3.x,你需要用 python3 运行

    Python 3.x:
    $python3 myScript.py
    当你安装包的时候,如果有可能安装到了 Python 2.x 而不是 Python 3.x 里,就需要使用:
    $sudo python3 setup.py install
    如果用 pip 安装,你还可以用pip3 安装 Python 3.x 版本的包:
    $pip3 install beautifulsoup4
    https://blog.csdn.net/DoctorLDQ/article/details/73230026


     解决方法有三中:

          1.在命令行修改,仅本会话有效:
             1)通过>>>sys.getdefaultencoding()查看当前编码(若报错,先执行>>>import sys >>>reload(sys));
             2)通过>>>sys.setdefaultencoding('utf8')设置编码

          2.较繁琐,最有效
             1)在程序文件中以下三句
                  import sys
                  reload(sys)
                  sys.setdefaultencoding('utf8')
          
          3.修改Python本环境(推荐)
             在Python的Libsite-packages文件夹下新建一个sitecustomize.py文件,内容为:
                 #coding=utf8
                 import sys
                 reload(sys)
                 sys.setdefaultencoding('utf8')

          重启Python解释器,发现编码已被设置为utf8,与方案二同效;这是因为系统在Python启动的时候,自行调用该文件,

    设置系统的默认编码,而不需要每次都手动加上解决代码,属于一劳永逸的解决方法。

    #在这里我是用的是以下方法,感觉很方便,但是最后一个我感觉很好一劳永逸不错不错,但是我没找着....
    import
    sys reload(sys) sys.setdefaultencoding('utf8')

     5、header的改变,这也是一处一定要注意的细节。

    如何获取网页的header{ ...}方法我还是简单说一下吧,会的自行略过,去自己电脑打开的网页copy吧。

    打开url,F12 然后 Ctrl+R   ,

    按照以下格式进行选取,写入我们的程序中。(不要漏掉逗号!!!!)

     header = {
                    'authority':'h5tq.moji.com',
                    'accept':'image/webp,image/apng,image/*,*/*;q=0.8',
                    'accept-encoding':'gzip, deflate, br',
                    'accept-language':'zh-CN,zh;q=0.9',
                    'cache-control':'no-cache',
                    'pragma':'no-cache',
                    'referer':'https://tianqi.moji.com/weather/china/shandong/penglai',
                    'user-agent':'Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/72.0.3626.121 Chrome/72.0.3626.121 Safari/537.36'
                    }

     6、....还没么,今下午几个小时整理的比较急 ,如果还有什么问题请留言评论或者私信我都可以。

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

    最后测试结果如下:

    大家应该也看到了有个很明显的BUG就是晴晴,

    但是经过我的测试,在python3下完全ok,毕竟3才是未来的霸主,2就不要花太多心思了。

    我上面也有pip3的教程,安装好了,把我们所需的库安装到位,打开方式切换为python3IDLE否则默认都是python2IDLE

    而且。。。而且python2中也没有运行结果没有输出,也许是因为python2 和 python3 的输出函数不一样所致,

    python2 中print 没有括号,python3中更加严谨规范带有括号。

    终于兑现了,舒心了不少....整理的不够到位的地方还请指正。

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

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

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

  • 相关阅读:
    光流法简单介绍
    learn something
    MOT
    jupyter notebook 启动出错
    SSD用测试集得到具体的检测结果
    百练_2677 肿瘤检测
    百练_2707 求一元二次方程的根
    百练_4022 买房子
    HDU2035 人见人爱A^B(快速幂)
    BestCoder Round #85 sum
  • 原文地址:https://www.cnblogs.com/wyl-pi/p/10637688.html
Copyright © 2020-2023  润新知