Chinaunix首页 | 论坛 | 博客
  • 博客访问: 661555
  • 博文数量: 139
  • 博客积分: 2655
  • 博客等级: 少校
  • 技术积分: 1723
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-02 16:03
文章分类

全部博文(139)

文章存档

2013年(2)

2011年(17)

2010年(14)

2009年(86)

2008年(20)

分类: Python/Ruby

2009-04-19 01:26:27

#!/usr/bin/env python
# -*- encoding:utf-8 -*-
#-------------------------------------------------------------------------
#FileName: weather_broadcast.py
#Description: automatically get the weather information from internet,deal with it and send it out everyday to my sisters,best friends and myself^_^. Actually, It can be sent out as short messages by the ChinaMobile Fetion which will be useful(Because of some reasons, I use chinaunicom now. I use email everyday,so It's enough for me...)
#Author: Xiang Guo(gx2008758@gmail.com)
#Time : Wed Mar 24 23:44:34 CST 2009
#Modified: Sun Apr 19 01:04:18 CST 2009
#(1).Fix the IOError and socket.gaierror
#(2).Make sure tomorrow'
s date displayed correctly
#(3).replace the handling of the file with pure python
#(4).Write a class to include the all functions
#-------------------------------------------------------------------------
import os,sys
import commands
import urllib,smtplib,email
import socket
import re

#urlbase = ""
urlbase = ""
cities={'苏州':'101190401','徐州':'101190801','南京':'101190101'}


class Weather:
    def __init__(self):
        pass
    #remove the old pages,not necessary
    #def rm_pages(city):
    # localfile=city + '.shtml'
    #     if os.path.isfile(localfile):
    # cmd='rm -f ./%s' %localfile
    # os.popen(cmd)
    #get_weather_message    
    def get_message(self,city):
    url=urlbase + cities[city] + '.shtml'
    localfile=city+'.shtml'
    while 1:
            try:
         urllib.urlretrieve(url,localfile)
        except IOError,e:
         continue
        else:
         break
        #delete the useless information
    f=open(localfile,"r")
    content = f.read()
    a=re.sub('\r\n','',content)
    a=re.sub('','',a)
    a=re.sub('c_1_2.*dd_0','',a,)
    a=re.sub('