import requests
import smtplib
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from pyquery import PyQuery as pq
def sendWeatherMail(url,to):
#获取天气信息
html = requests.get(url).content
doc = pq(html)
clearfix1 = doc('#today .clearfix')
clearfix2 = clearfix1.remove('.sky').text()
a = clearfix2.split()
wea = str(a[0]+':\n'+a[1]+' '+a[2]+' 风速:'+a[3]+' '+a[4]+' '+a[5]+'\n\n'+a[6]+':\n'+a[7]+' '+a[8]+' 风速:'+a[9]+' '+a[10]+' '+a[11])
#发送邮件
msg = MIMEText(wea)
msg['Subject'] = 'XX地区天气预报'
msg['From'] = 'admin@XXXXX.com' #邮箱账号
msg['To'] = to
mail_host = 'smtpdm.aliyun.com' #我这里用的阿里云推送 163 QQ 均可
mail_user = 'admin@XXXXX.com' #自己的邮箱账号
mail_pass = 'XXXXXXXXXXXX' #你的那一串key
s = smtplib.SMTP_SSL(mail_host)
s.connect(mail_host, 465)
s.login(mail_user,mail_pass)
s.send_message(msg)
s.quit()
try:
sendWeatherMail('http://www.weather.com.cn/weather1d/101010300.shtml#input','86593927@qq.com')#101010300为北京市朝阳区城市编号 可更改地区编号 86593927@qq.com改为发送对象的邮箱地址
print('邮件发送成功')
except smtplib.SMTPException:
print('邮件发送失败')
版权属于:
怪怪的科长
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论