BeautifulSoup爬取天气信息并发送至QQ邮箱

"""
    BeautifulSoup爬取天气信息并发送至QQ邮箱
"""
import smtplib
import urllib.request
from bs4 import BeautifulSoup
from email.mime.text import MIMEText


url = "http://www.weather.com.cn/weather/101040100.shtml"
content = urllib.request.urlopen(url).read()
soup = BeautifulSoup(content, "html.parser")

content = ""
name = soup.find_all(attrs={"class": "sky skyid lv3 on"})
for u in name:
    wea = u.find(attrs={"class": "wea"}).get_text()
    tem = u.find(attrs={"class": "tem"}).get_text()
    content = "天气:" + wea + " 温度:" + tem
    content = content.replace("\n", "")
    print(content)

msg_from = "962565593@qq.com"
EMAIL_HOST_PASSWORD = 'hemdjqjmvcqjbfbc'
msg_to = "1933762162@qq.com"
subject = "重庆天气预报"
other = content + "\n媳妇儿,照顾好自己哦!!!"
print(other)

msg = MIMEText(other, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to

try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.set_debuglevel(1)
    s.login(msg_from, EMAIL_HOST_PASSWORD)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print("发送成功")
except Exception as e:
    print("发送失败")
    print(e)
finally:
    s.quit()


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值