#由于每天早上要和妹子说早安,于是做个定时任务,每天早上自动爬取天气,发送天气问好邮件
#
#涉及模块:
#(1)定时任务:windows的定时任务
#             配置教程链接:http://blog.csdn.net/wwy11/article/details/51100432
#(2)爬取天气:用的是中国天气网   http://www.weather.com.cn/weather/101190101.shtml   101190101为城市id,动态获取
#        爬虫代码见上一篇博客 http://www.cnblogs.com/chenyuebai/p/6728532.html
#(3)发送邮件:代码同在上一篇博客
#(4)结束处理:笔记本自动关机,代码同在上一篇博客    #os.system('shutdown -s -t 1')

#20170427 加入失败重试;优化邮件正文

################################################################# 
#author: 陈月白
#_blogs: http://www.cnblogs.com/chenyuebai/
################################################################# # -*- coding: utf-8 -*-
import sys
import time
import os
import traceback
import crawler_tools_01 # curPath = os.path.abspath(os.path.dirname(__file__))
# sys.path.append(curPath) city_code_dic = {
"南京": "",
"北京": ""
} class MORNING(crawler_tools_01.CRAWLER):
#获取城市id,返回url
def get_wertherUrl_by_cityName(self, cityName):
cityId = city_code_dic[cityName]
if cityId == "":
print("get cityId failed,use default:101010100 " % cityName)
wertherUrl = "http://www.weather.com.cn/weather/" + "" + ".shtml"
return wertherUrl
else:
wertherUrl = "http://www.weather.com.cn/weather/" + cityId + ".shtml"
# print(wertherUrl)
return wertherUrl #获取天气信息
def get_today_weather_by_weatherUrl(self,weatherUrl):
flag_today = '<li class="sky skyid lv2 on">.*?<h1>(.*?)</h1>.*?</big>.*?title=(.*?)class.*?<span>(.*?)</span>.*?<i>(.*?)</i>.*?span title=(.*?)class=.*?<i>(.*?)</i>'
items_today_tmp = self.select_items_from_url(weatherUrl,flag_today) #获取页面信息失败重试一次
if not items_today_tmp:
items_today_tmp = self.select_items_from_url(weatherUrl,flag_today)
print("items_today_tmp =",items_today_tmp) #数据处理 元组转列表
items_today = []
try:
for i in items_today_tmp[0]:
items_today.append(i)
print("items_today =", items_today)
return items_today
except:
traceback.print_exc()
print("CATCH AN ERROR AT:items_today_tmp transTo items_today")
return items_today_tmp def make_mail_body(self,items_today):
try:
body_text = "美好的一天,从我的问候开始~~~\n \n今日天气:\n%s: %s 温度:%s 至 %s %s %s\n \n \n请根据温度注意穿衣,阴雨天记得带伞 \n from Mr.ch"%(items_today[0], items_today[1], items_today[2], items_today[3], items_today[4], items_today[5])
return body_text
except:
traceback.print_exc()
body_text = "美好的一天,从我的问候开始~~~\n \n今日天气:%s\n \n \n请根据温度注意穿衣,阴雨天记得带伞 \n \n from Mr.ch" % items_today
return body_text def main():
ZMJ = MORNING()
weatherUrl = ZMJ.get_wertherUrl_by_cityName("南京")
print("01 weatherUrl =", weatherUrl)
# 获取今日天气信息
items_today = ZMJ.get_today_weather_by_weatherUrl(weatherUrl)
#生成邮件正文
body_text = ZMJ.make_mail_body(items_today) #发送邮件
date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
#ZMJ.send_email(["50*******@qq.com"], "爱心天气预报_%s"%date,body_text) ZMJ.send_email(["50*******@qq.com","46********@qq.com"], "爱心天气预报_%s"%date,body_text)
ZMJ.shutdown(10) main()

运行结果:

python 3.6 urllib库实现天气爬取、邮件定时给妹子发送天气的更多相关文章

  1. 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...

  2. 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...

  3. 使用Python自带的库和正则表达式爬取熊猫直播主播观看人气

    主要是体现代码的规范性 from urllib import request import re class Spider(): url = 'https://www.panda.tv/cate/lo ...

  4. python 爬虫之 urllib库

    文章更新于:2020-03-02 注:代码来自老师授课用样例. 一.初识 urllib 库 在 python2.x 版本,urllib 与urllib2 是两个库,在 python3.x 版本,二者合 ...

  5. python爬虫之urllib库(三)

    python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...

  6. python爬虫之urllib库(二)

    python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...

  7. python爬虫之urllib库(一)

    python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...

  8. [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息

    [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2018-07-21 23:53:02 larger5 阅读数 4123更多 分类专栏: 网络爬虫   版权声明: ...

  9. Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(人人网)(下)

    Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(下) 自动使用cookie的方法,告别手动拷贝cookie http模块包含一些关于cookie的模块,通过他们我们可以自动的使用co ...

随机推荐

  1. JavaScript变量存储浅析

    var test=100; function foo(){ console.log(test);//undefined var test=200; console.log(test); } 我们明明定 ...

  2. 混合app

    cordova run android            把应用发送到手机   ionic serve        电脑浏览器调试命令   创建: cordova create hello co ...

  3. Linux修改date

    与cn校时 ntpdate cn.pool.ntp.org 设置时区 TZ='Asia/Shanghai'; export TZ 刷新生效 source ~/.bashrc vi /etc/profi ...

  4. Java--集合(一)

    一.前序     前几篇文章我结合数据结构说了一些常用的集合,但是我感觉那样可能不系统,于是乎想着重写,按照由整体到细节的方式去写,这样才能更好的把握集合,废话不多说开始吧: 二.集合框架      ...

  5. 一起写框架-MVC框架-基础功能-Date类型数据绑定(七)

    实现功能 表单请求传递的数据,格式为以下格式的日期时间数据. (1):yyyy-MM-dd hh:mm:ss (2):yyyy-MM-dd 执行方法可以使用Date类型接收. 实现思路 1.获得表单字 ...

  6. C#中访问私有成员

    首先访问一个类的私有成员不是什么好做法.大家都知道私有成员在外部是不能被访问的.一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员造访,可以套用下面这种非常好的方式去解决. pr ...

  7. Kotlin编码----var和val的区别

    var是一个可变变量,这是一个可以通过重新分配来更改为另一个值的变量.这种声明变量的方式和Java中常规的变量的声明方式一样. val是一个只读变量,这种声明变量的方式相当于java中的final变量 ...

  8. shell 备份脚本

    [root@izwz9hmoz58gvtu0ldpm0iz ~]# cat /usr/local/aaaa/shell_script/Mysql_Dump_LJY.sh #! /bin/bash to ...

  9. Kaggle实战之二分类问题

    0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 "尽管新技术新算法层出不穷,但是掌握好基础算法就能解决手 ...

  10. 开源项目 log4android 使用方式详解

    话不多说, 直接上主题. log4android 是一个类似于log4j的开源android 日志记录项目. 项目基于 microlog 改编而来, 新加入了对文件输出的各种定义方式. 项目地址: 点 ...