利用Python發送天氣預報資訊到手機__Python

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.