Python发送天气预报信息到手机
	
    
    
	 writeblog.csdn.net writeblog.csdn.net/PostEdit.aspx  
这个程序很早以前就写过了,而且是参考的别人的写,具体谁的发在哪里我都忘记了。这里就算是半原创了,如有侵权请及时通知改正。
因为从今天1月1号开始,Google上订阅的天气预报服务已经取消了,估计是Google被施加压力了。反正是收不到天气预报了。正好重拾以前的那个脚本,自己设置抓取信息并发到手机就行了。
之前的脚本是用Python写的,抓的是新浪天气预报页面的信息,使用cocobear提供的PyFetion发送到自己手机上。上周拿来一运行,报error...
原来是飞信平台升级了,PyFetion也跟着升级了,而且新浪天气预报的页面也改版了。好嘛。。。
换用ip138提取的天气信息,重新改写如下
 # -*- 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('''<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.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], theG
    
     
	
	
    
    
	相关文档:
        
    
    16.1. select — Waiting for I/O completion¶ 
This module provides access to the select and poll functions available in most operating systems, epoll available on Linux 2.5+ and kqueue available on most BSD. Note that on Windows, it only works for sockets; on other operating systems, it al ......
	
    
        
    
    8.8. queue — A synchronized queue class¶
queue -- 一个同步队列类
The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implemen ......
	
    
        
    
         前一段时间试着用这三种语言简单的写了关于文件拷贝的程序,发现c#和python的api惊人的相似,对于文件的操作这两种语言非常的方便。都没有加异常的处理
C#源代码:
        public static void CopyFile(string source, string destination)
  & ......
	
    
        
    
    import random   def windex(lst):   
    '''an attempt to make a random.choose() function that makes weighted choices    
    accepts a list of tuples with the item and probability as a pair'''    
    wtotal = sum([x[1] for x in lst])    
   ......