#由于每天早上要和妹子说早安,于是做个定时任务,每天早上自动爬取天气,发送天气问好邮件
#
#涉及模块:
#(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. HDU 4291 A Short problem(矩阵+循环节)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. Picture

    Picture Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. 楼梯T-SQL:超越基础6级:使用CASE表达式和IIF函数

     从他的楼梯到T-SQL DML,Gregory Larsen涵盖了更多的高级方面的T-SQL语言,如子查询. 有时您需要编写一个可以根据另一个表达式的评估返回不同的TSQL表达式的单个TSQL语句. ...

  4. python3 字符串操作相关函数

    整理自python基础|菜鸟教程 感谢菜鸟教程提供的优质资源! 1.capitalize() 将字符串的第一个字符转换为大写 实例 以下实例展示了capitalize()方法的实例: #!/usr/b ...

  5. 利用echarts highcharts 实现自定义地图 关系图效果 侧边3D柱形图饼图散点图

    github 地址:  https://https://github.com/Gengshaoxuan/medataMap github 地址:  https://https://github.com ...

  6. nohup和&后台运行,查看占用端口进程

    1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg - ] [ & ] 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup ...

  7. vmware中Ubuntu不能全屏展示的问题

    依次打开system settings---------------->Displays----------------->resoluiton调整分辨率,然后右下角点击apply,然后k ...

  8. [转载] 说说JSON和JSONP,也许你会豁然开朗

    转载自http://kb.cnblogs.com/page/139725/ 前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了 ...

  9. iPhone X 网页导航概念

     以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   在移动应用程序设计中,选择汉堡菜单按钮还是标签栏作为导航一直是个古老的争论话题.目前看来,由于 ...

  10. 深入理解js中的apply、call、bind

    概述 js中的apply,call都是为了改变某个函数运行时的上下文环境而存在的,即改变函数内部的this指向. apply() apply 方法传入两个参数:一个是作为函数上下文的对象,另外一个是作 ...