一个非常简单的获取城市天气转发邮箱的简单代码

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('邮件发送失败')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值