python爬取天气预报用163邮箱发

我只是一个虾纸丫 提交于 2020-03-06 12:43:26
import smtplib
import pandas as pd
import numpy as np
import requests
from lxml import etree
from email.mime.text import MIMEText


def parse(url = 'https://www.tianqi.com/shanghai'):
    headers = {'User-Agent':'Mozila/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    html = requests.get(url,headers = headers)
    bs = etree.HTML(html.text)

    #今天天气相关数据:日期,星期几,天气,最低气温,最高气温
    today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0]
    today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0]
    today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0]
    today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0]
    today_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0]

    #明天天气相关数据,维度和上述一致
    tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0]
    tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0]
    tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0]
    tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0]
    tomorrow_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0]

    tomorrow = ('明天是%s,%s,%s,%s-%s度,温差%d度')% \
          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low)))

    print(('明天是%s,%s,%s,%s-%s度,温差%d度')% \
          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low))))

    #计算今明两天温度差异,这里用的是最高温度
    temperature_distance = int(tomorrow_high) - int(today_high)

    if temperature_distance > 0:
        a = '明日升温%d' % temperature_distance
        print('明日升温%d' % temperature_distance)
    if temperature_distance < 0:
        a = '明日降温%d' % temperature_distance
        print('明日降温%d' % temperature_distance)
    else:
        a = '最高气温不变'
        print('最高气温不变')

    content = tomorrow,a
    return content

weather = parse()


# 第三方 SMTP 服务
mail_host = "smtp.163.com"  # SMTP服务器
mail_user = "1*****@163.com"  # 用户名
mail_pass = "123456"  # 密码

sender = '1****@163.com'  # 发件人邮箱
receivers = ['1*****@163.com']  # 接收邮件,可设置为你的其他邮箱,如果是QQ邮箱,需要开启stmp
  ##############使用qq邮箱的时候,记得要去开启你的qq邮箱的smtp服务;##############
    # 方法:
    # 1)登录到你的qq邮箱;
    # 2)找到首页顶部的【设置】并点击;
    # 3)找到【账户】这个选项卡并点击,然后在页面中找到“SMTP”相关字样,找到【开启】的超链接,点击后会告诉你开启方法(需要发个短信),然后按照指示操作,最终会给你一个密码,这个密码可以用于在代码中当作邮箱密码
    # 
    ###########################################################################
    
sss = ''.join(weather)#将 tuple 转换为string
title = 'Python SMTP Mail Test'  # 邮件主题
message = MIMEText(sss, 'plain', 'utf-8')  # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
    smtpObj.login(mail_user, mail_pass)  # 登录验证
    smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
    print("mail has been send successfully.")
except smtplib.SMTPException as e:
    print(e)


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!