Python发送天气预报信息得手机
Python发送天气预报信息到手机
这个程序很早以前就写过了,而且是参考的别人的写,具体谁的发
-*- coding:utf-8 -*- # file:weather.py # by Lee, 2010-1-11 """ 抓取天气预报信息,并通过pyfetion发送短信通知 """ import os import re import urllib import sys import time
from PyFetion import * def GetWeather(): try: # 获取网页源文件 sock = urllib.urlopen("http://qq.ip138.com/weather/guangdong/DongGuan.htm") strhtml = sock.read() strhtml = unicode(strhtml, 'gb2312','ignore').encode('utf-8','ignore') # 正则式取温度信息 theGrades = re.findall('''(\d+)℃''', strhtml) # 获取天气描述信息 weathers = re.findall(''' (.*)''',strhtml) 25 # 定义时间格式 26 this_date = str(time.strftime("%Y/%m/%d %a")) 27 now = int(time.time()) 28 sec = 24*60*60 29 day_today = "今天(%s号)" % str(time.strftime("%d", time.localtime(now+0*sec))) 30 day_tommo = "明天(%s号)" % str(time.strftime("%d", time.localtime(now+1*sec))) 31 day_aftom = "后天(%s号)" % str(time.strftime("%d", time.localtime(now+2*sec))) 32 # 定义短信正文 33 sms = [this_date] 34 sms.append("东莞天气") 35 sms.append("%s:%s, %s-%s℃" % (day_today, weathers[0], theGrades[1], theGrades[0])) 36 sms.append("%s:%s, %s-%s℃" % (day_tommo, weathers[1], theGrades[3], theGrades[2])) 37 sms.append("%s:%s, %s-%s℃" % (day_aftom, weathers[2], theGrades[5], theGrades[4])) 38 sms.append("天气有冷暖,关怀永不变!") 39 #sms.append("测试中,收到请MSN回复我,谢谢~~") 40 #sms.append("欢迎对短信格式和每天定点发送时间提出宝贵意见") 41 smscontent = '\n'.join(sms) 42 #print len(smscontent) # check length of sms fetion 43 return smscontent #.decode('utf-8').encode('gb2312') 44 except: 45 return "There is sth wrong with the weather forecast, please inform the author. thx~" 46 47 def SendSMS(sms): 48 myphone = '1589xxxxx67' # 手机号 49 mypwd = 'xxxx' # 登录密码 50 destphone = ["62487xxxx", "70078xxxx", "69451xxxx"] # 发送对象飞信号 51 # 发送目的地改为飞信号,之前用的手机号做目的地发送,别人的收不到短信 52 print "\nwaiting for login fetion..." 53 fetion = PyFetion(myphone, mypwd, 'TCP') 54 fetion.login(FetionHidden) 55 for phone in destphone: 56 print "sending to", phone 57 fetion.send_sms(sms, phone, True) 58 print "OK" 59 fetion.logout() 60 return True 61 62 def main(): 63 print "getting out the weather code..." 64 msg = GetWeather() 65 print "\n", msg 66 # SendSMS("测试天气预报") 67 SendSMS(msg) 68 print "Done." 69 70 if __name__ == "__main__": 71 sys.exit(main())在哪里我都忘记了。这里就算是半原创了,如有侵权请及时通知改正。
因为从今天1月1号开始,Google上订阅的天气预报服务已经取消了,估计是Google被施加压力了。反正是收不到天气预报了。正好重拾以前的那个脚本,自己设置抓取信息并发到手机就行了。
之前的脚本是用Python写的,抓的是新浪天气预报页面的信息,使用cocobear提供的PyFetion发送到自己手机上。上周拿来一运行,报error...
原来是飞信平台升级了,PyFetion也跟着升级了,而且新浪天气预报的页面也改版了。好嘛。。。
换用ip138提取的天气信息,重新改写如下