Python傳送天氣預報資訊到手機

2021-08-31 10:12:53 字數 2690 閱讀 5317

這個程式很早以前就寫過了,而且是參考的別人的寫,具體誰的發

-*- 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("")

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]

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 = '1589***xx67' # 手機號

49 mypwd = '***x' # 登入密碼

50 destphone = ["62487***x", "70078***x", "69451***x"] # 傳送物件飛訊號

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被施加壓力了。反正是收不到天氣預報了。正好重拾以前的那個指令碼,自己設定抓取資訊併發到手機就行了。

換用ip138提取的天氣資訊,重新改寫如下

Google API 獲取天氣預報資訊

要獲得天氣預報資訊可用利用google提供的藉口,城市名稱 拼音 它將返回乙個用xml封裝的天氣資訊。示例 shanghai 返回 xml version 1.0 xml api reply version 1 weather module id 0 tab id 0 mobile row 0 mo...

Google API 獲取天氣預報資訊

要獲得天氣預報資訊可用利用google提供的藉口,城市名稱 拼音 它將返回乙個用xml封裝的天氣資訊。示例 shanghai 返回 xml version 1.0 xml api reply version 1 weather module id 0 tab id 0 mobile row 0 mo...

獲取各地天氣預報資訊

出門旅遊時,或是在一些手機流量耗盡 無法上網的時候,可通過在臨走時定製一些天氣資訊,來傳送!主要是通過linux cron fetion api 網頁解析 提供天氣資訊的 1.m.sohu.com 有乙個city引數,傳入encode 北京 來查詢當天的天氣,做相應解析即可 2.這需要乙個它所定義的...