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 = { "南京": "101190101", "北京": "101010100" } 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/" + "101010100" + ".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()

 

 

運行結果:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM