代码实现
这里爬取天气信息比较简单所以就直接附上代码了
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import requests
from lxml import etree
# 获取当天的天气情况
def ForWeather(weather_list):
today=[]
for weather in weather_list:
dic={}
#标题
weather_title=weather.xpath('./h1/text()')[0]
#天气
wea=weather.xpath('./p[@class="wea"]/@title')[0]
sky=weather.xpath('string(./div[@class="sky"])')
#温度
tem=weather.xpath('string(./p[@class="tem"])')
#风速
win=weather.xpath('./p[@class="win"]/span/@title')[0]
speed=weather.xpath('./p[@class="win"]/span/text()')[0]
sun=weather.xpath('string(./p[4])').strip()
dic['weather_title']=weather_title
dic['wea']=wea
dic['sky']=sky.replace("\n"," ")
dic['tem']=tem.strip()
dic['win']=win
dic['speed']=speed
dic['sun']=sun
today.append(dic)
return today
#获取当天生活指数
def IndexForLife(life_list):
life={}
#中暑指数
heatstroke='\n'.join([life_list[0].xpath('.//span/text()')[0],"推荐:"+life_list[0].xpath('.//p/text()')[0]])
#减肥指数
reduce_weight = '\n'.join([str(len(life_list[1].xpath('.//span/em')))+"颗星", "推荐:"+life_list[1].xpath('.//p/text()')[0]])
#血糖指数
blood_sugar='\n'.join([life_list[2].xpath('.//span/text()')[0],"推荐:"+life_list[2].xpath('.//p/text()')[0]])
#穿衣指数
dress = '\n'.join([life_list[3].xpath('.//span/text()')[0],"推荐:"+life_list[3].xpath('.//p/text()')[0]])
#洗车指数
wish_car = '\n'.join([life_list[4].xpath('.//span/text()')[0], "推荐:"+life_list[4].xpath('.//p/text()')[0]])
#紫外线指数
ultraviolet = '\n'.join([life_list[5].xpath('.//span/text()')[0],"推荐:"+ life_list[5].xpath('.//p/text()')[0]])
life['heatstroke']=heatstroke
life['reduce_weight']=reduce_weight
life['blood_sugar']=blood_sugar
life['dress']=dress
life['wish_car']=wish_car
life['ultraviolet']=ultraviolet
return life
# 获取当天的天气情况
def GetOnSameDayWeather():
url='http://www.weather.com.cn/weather1d/101250101.shtml'
rep=s.get(url).content.decode(encoding='utf-8')
tree=etree.HTML(rep)
weather_list=tree.xpath('//ul[@class="clearfix"]/li')[:2]
life_list=tree.xpath('//ul[@class="clearfix"]/li')[2:]
#获取当天白天和夜间天气情况
today=ForWeather(weather_list)
#获取当天生活指数
life=IndexForLife(life_list)
result=''
for t in today:
result+=f'' \
f'时间点:{t["weather_title"]}\n' \
f'天气:{t["wea"]}\n' \
f'天空:{t["sky"]}\n' \
f'平均温度:{t["tem"]}\n' \
f'风速:{t["win"]}:{t["speed"]}\n' \
f'日出/日落:{t["sun"]}\n'
result +=f'' \
f'中暑指数:{life["heatstroke"]}\n'\
f'减肥指数:{life["reduce_weight"]}\n'\
f'血糖指数:{life["blood_sugar"]}\n'\
f'穿衣指数:{life["dress"]}\n'\
f'洗车指数:{life["wish_car"]}\n'\
f'紫外线指数:{life["ultraviolet"]}\n'
return result
# 获取七天内的天气情况
def ForSevenDaysWeather():
url = 'http://www.weather.com.cn/weather/101250101.shtml'
rep = s.get(url).content.decode(encoding='utf-8')
tree = etree.HTML(rep)
weather_list = tree.xpath('//ul[@class="t clearfix"]/li')
week=[]
for li in weather_list:
dic = {}
#时间
day=li.xpath('./h1/text()')[0]
# 天气
wea = li.xpath('./p[@class="wea"]/@title')[0]
# 温度
tem = li.xpath('string(./p[@class="tem"])')
# 风速
win = li.xpath('./p[@class="win"]//span/@title')[0]
speed = li.xpath('./p[@class="win"]/i/text()')[0]
dic['day'] = day
dic['wea'] = wea
dic['tem'] = tem.strip()
dic['win'] = win
dic['speed'] = speed
week.append(dic)
result=''
for t in week:
result += f'' \
f'时间点:{t["day"]}\n' \
f'天气:{t["wea"]}\n' \
f'平均温度:{t["tem"]}\n' \
f'风速:{t["win"]}:{t["speed"]}\n'+'-'*50+'\n'
return result
#发送邮件
def sendEmail(content,title):
sender="自己的163邮箱"
receivers=["*****@qq.com"]
mail_host = "smtp.163.com"
mail_pass="密码"
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(sender, mail_pass) # 登录验证
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
print("mail has been send successfully.")
except smtplib.SMTPException as e:
print(e)
if __name__ == '__main__':
s = requests.session()
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.6.1000",
}
# 获取当天的天气情况
Today_result=GetOnSameDayWeather()
#发送邮箱
sendEmail(Today_result,'今日天气情况')
# 获取七天内的天气情况
Week_result=ForSevenDaysWeather()
# 发送邮箱
# sendEmail(Week_result,'一周的天气情况')
如果出现什么问题邮箱发送失败,可能用qq邮箱发给163邮箱
window设置定时任务
打开管理
使用快捷键“windows+r”打开运行指令框。
输入:compmgmt.msc
创建基本任务
填写信息
定义开始时间(用户根据需求来定)