利用Python发送天气预报信息到手机

来源:互联网 发布:21天学通linux c编程 编辑:程序博客网 时间:2024/05/10 04:40

#!/usr/bin/env python
#coding=utf-8

import urllib, urllib2
import re
import time
#from PyFetion import *

proxy=urllib2.ProxyHandler({'http': 'http://lk:2002@172.17.5.53:80'})
opener=urllib2.build_opener(proxy)
urllib2.install_opener(opener)


def GetWeather():
sock = urllib2.urlopen("http://qq.ip138.com/weather/guangdong/DongGuan.htm")
strhtml = sock.read()
strhtml = unicode(strhtml, 'gb2312','ignore').encode('utf-8','ignore')
#strhtml = unicode(strhtml, 'gb2312','ignore').encode('big5','ignore')
#print strhtml

theGrades = re.findall('<td>(.*℃)</td>', strhtml)
weathers = re.findall('<td><img src="/image/b/d+.gif" alt=".*" /><br/>(.*)</td>', 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 = []
sms.append("東莞天气")
sms.append("%s:%s, %s" % (day_today, weathers[0], theGrades[0]))
sms.append("%s:%s, %s" % (day_tommo, weathers[1], theGrades[1]))
sms.append("%s:%s, %s" % (day_aftom, weathers[2], theGrades[2]))
sms.append("天气有冷暖,關怀永不變!")

smscontent = '/n'.join(sms)
return smscontent


def SendSMS(sms):
myphone = '1365024****' # 手机号
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__":
main()

 

原创粉丝点击