python爬取天氣預報用163郵箱發

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)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章