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 ...
随机推荐
- Fibonacci(...刷的前几道题没有记博客的习惯,吃了大亏)
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Even Tree 小议
原题链接:https://www.hackerrank.com/challenges/even-tree/problem 思路:把树还原出来,对每个结点,计算以该结点为根的子树结点数.子树结点数为偶数 ...
- Azkaban安装部署
在root的用户下搭建的 • Azkaban安装部署(可参照:http://azkaban.github.io/azkaban/docs/latest/) 1):前提 安装JDK,安装Hadoop,H ...
- jQuery选择器(ID选择器)第一节
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- mkdir--命令
mkdir命令 mkdir:make directory(ies)的缩写,用来创建目录 1.语法 mkdir [OPTION]... DIRECTORY 注释:option是选择,可选,directo ...
- 对比MFC和Winform及WPF
MFC 生成本机代码,自然是很快.可是,消息循环,减缓了界面显示速度.winform 封装了 win32 的api,多次进行P/invoke 操作 (大部分使用p/invoke操作封装),速度慢 .w ...
- C#移位运算(左移和右移)
C#是用<<(左移) 和 >>(右移) 运算符是用来执行移位运算. 左移 (<<) 将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0. 左移相当于乘. ...
- C语 三子棋小游戏
#include <stdio.h> #include <Windows.h> #include<time.h> #define row 3 #define lis ...
- 结合程序崩溃后的core文件分析bug
引言 在<I/O的效率比较>中,我们在修改图1程序的BUF_SIZE为8388608时,运行程序出现崩溃,如下图1: 图1. 段错误 一般而言,导致程序段 ...
- UNIX标准及实现
UNIX标准及实现 引言 在UNIX编程环境和C程序设计语言的标准化方面已经做了很多工作.虽然UNIX应用程序在不同的UNIX操作系统版本之间进行移植相当容易,但是20世纪80年代UNIX版本 ...