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'] = 'Today is a beautifulday!'
msg['From'] = '378275896@qq.com'
msg['To'] = to
mail_host = 'smtp.qq.com'
mail_user = '378275896@qq.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/101020100.shtml#input','378275896@qq.com')
print('邮件发送成功')
except smtplib.SMTPException:
print('邮件发送失败')
一个非常简单的获取城市天气转发邮箱的简单代码
最新推荐文章于 2024-07-30 21:09:58 发布