python 推送消息到手机_Python发送天气预报信息到手机

该Python脚本抓取东莞的天气信息,通过PyFetion库发送短信通知,包括当前及未来两天的天气和温度。首先从指定网站获取天气数据,然后使用正则表达式解析温度和天气描述,最后将信息整合成短信内容并发送。
摘要由CSDN通过智能技术生成

# -*- 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)

# 定义时间格式

this_date = str(time.strftime("%Y/%m/%d %a"))

now = int(time.time())

sec = 24*60*60

day_today = "今天(%s号)" % str(time.strftime("%d", time.localtime(now+0*sec)))

day_tommo = "明天(%s号)" % str(time.strftime("%d", time.localtime(now+1*sec)))

day_aftom = "后天(%s号)" % str(time.strftime("%d", time.localtime(now+2*sec)))

# 定义短信正文

sms = [this_date]

sms.append("东莞天气")

sms.append("%s:%s, %s-%s℃" % (day_today, weathers[0], theGrades[1], theGrades[0]))

sms.append("%s:%s, %s-%s℃" % (day_tommo, weathers[1], theGrades[3], theGrades[2]))

sms.append("%s:%s, %s-%s℃" % (day_aftom, weathers[2], theGrades[5], theGrades[4]))

sms.append("天气有冷暖,关怀永不变!")

#sms.append("测试中,收到请MSN回复我,谢谢~~")

#sms.append("欢迎对短信格式和每天定点发送时间提出宝贵意见")

smscontent = '\n'.join(sms)

#print len(smscontent) # check length of sms fetion

return smscontent #.decode('utf-8').encode('gb2312')

except:

return "There is sth wrong with the weather forecast, please inform the author. thx~"

def SendSMS(sms):

myphone = '1589xxxxx67' # 手机号

mypwd = 'xxxx' # 登录密码

destphone = ["62487xxxx", "70078xxxx", "69451xxxx"] # 发送对象飞信号

# 发送目的地改为飞信号,之前用的手机号做目的地发送,别人的收不到短信

print "\nwaiting for login fetion..."

fetion = PyFetion(myphone, mypwd, 'TCP')

fetion.login(FetionHidden)

for phone in destphone:

print "sending to", phone

fetion.send_sms(sms, phone, True)

print "OK"

fetion.logout()

return True

def main():

print "getting out the weather code..."

msg = GetWeather()

print "\n", msg

# SendSMS("测试天气预报")

SendSMS(msg)

print "Done."

if __name__ == "__main__":

sys.exit(main())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值