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

#!/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()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值