python 3.6 urllib库实现天气爬取、邮件定时给妹子发送天气
#由于每天早上要和妹子说早安,于是做个定时任务,每天早上自动爬取天气,发送天气问好邮件
#
#涉及模块:
#(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库实现天气爬取、邮件定时给妹子发送天气的更多相关文章
- 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解
第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...
- 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解
封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...
- 使用Python自带的库和正则表达式爬取熊猫直播主播观看人气
主要是体现代码的规范性 from urllib import request import re class Spider(): url = 'https://www.panda.tv/cate/lo ...
- python 爬虫之 urllib库
文章更新于:2020-03-02 注:代码来自老师授课用样例. 一.初识 urllib 库 在 python2.x 版本,urllib 与urllib2 是两个库,在 python3.x 版本,二者合 ...
- python爬虫之urllib库(三)
python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...
- python爬虫之urllib库(二)
python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...
- python爬虫之urllib库(一)
python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...
- [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息
[Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2018-07-21 23:53:02 larger5 阅读数 4123更多 分类专栏: 网络爬虫 版权声明: ...
- Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(人人网)(下)
Python爬虫教程-13-爬虫使用cookie爬取登录后的页面(下) 自动使用cookie的方法,告别手动拷贝cookie http模块包含一些关于cookie的模块,通过他们我们可以自动的使用co ...
随机推荐
- C++中模板template <typename T>
最近在看C++的源码,遇到了不少问题,一点一点进行补充. 首先就是遇到template <typename Dtype>. 网上解释的非常多,觉得比较啰嗦,其实就是一个类型模板. 比如我们 ...
- 修改Jenkins用户的密码
说明:本方法仅适用于jdk6+.tomcat6+和Jenkins专有用户数据库的Jenkins! 很多童鞋在使用jenkins的时候忘记密码了,然后各种蛋疼.最近闲着无事,折腾了下.好了,闲话少扯. ...
- H5缓存
h5 namifest(已经被废弃):http://www.tuicool.com/articles/VvABB3Y service worker缓存:https://developer.mozill ...
- svg snap 笔记
路径中的字母,大写相对于左上角绝对定位,小写相对定位 M110,95,95,110M115,100,100,115 pattern 类似于图片拼贴,可以把指定位置的图案用来填充 var patt ...
- display: run-in
If a sibling block box (that does not float and is not absolutely positioned) follows the run-in box ...
- kafka学习笔记1:测试环境搭建
最近因为架构中引入了kafka,一些之前在代码中通过RPC调用强耦合但是适合异步处理的内容可以用kafka重构一下. 考虑从头学一下kafka了解其特性和使用场景. 环境选择 首先是测试环境的搭建,平 ...
- Spring Boot单元测试(Mock)
Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...
- 《天书夜读:从汇编语言到windows内核编程》六 驱动、设备、与请求
1)跳入到基础篇的内核编程第7章,驱动入口函数DriverEnter的返回值决定驱动程序是否加载成功,当打算反汇编阅读驱动内核程序时,可寻找该位置. 2)DRIVER_OBJECT下的派遣函数(分发函 ...
- asp.net web api客户端调用
服务接口 接口1: //Post:http://127.0.0.1/HY_WebApi/api/V2/Key/FunctionTest1 [HttpPost] public HttpResponseM ...
- [转载] Solr使用入门指南
转载自http://blog.csdn.net/liuzhenwen/article/details/4060922 由于搜索引擎功能在门户社区中对提高用户体验有着重要的作用,在门户社区中涉及大量需要 ...