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('邮件发送失败')
一个非常简单的获取城市天气转发邮箱的简单代码
最新推荐文章于 2025-12-11 08:58:14 发布
本文介绍了一个使用Python编写的天气信息抓取及邮件通知程序。该程序利用requests库从指定URL获取天气信息,通过BeautifulSoup和PyQuery解析网页,然后使用smtplib库发送天气详情到指定邮箱。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.11
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
2565

被折叠的 条评论
为什么被折叠?



