主要任务:抓取杭州气象台网站上的天气预报,并张贴在BBS上某个指定板面,并提供了详尽的日志功能,供统计分析。

Python代码(2010-6-23更新中文折行部分)
  1. while len(line) – pos > 0:   
  2.     fix = 0  
  3.     part = line[pos:pos+limit+fix]   
  4.     next = line[pos+limit+fix:]   
  5.     lenlength = len(part.encode(‘gbk’))   
  6.     left = len(next.encode(‘gbk’))   
  7.   
  8.     while (length<limit*2) and (left>0):   
  9.         fix += 1   
  10.         part = line[pos:pos+limit+fix]   
  11.         next = line[pos+limit+fix:]   
  12.         lenlength = len(part.encode(‘gbk’))   
  13.         left = len(next.encode(‘gbk’))   
  14.     if (length>limit*2):   
  15.         fix 1  
  16.         part = line[pos:pos+limit+fix]   
  17.         next = line[pos+limit+fix:]   
  18.         left = len(next.encode(‘gbk’))   
  19.     point = part[-1:]   
  20.     if (point in u’〔〈《【(“‘’):   
  21.         fix 1  
  22.         part = line[pos:pos+limit+fix]   
  23.     elif (left>0):   
  24.         point = line[pos+limit+fix]   
  25.         if (point in u’?!:;、,。”’〕〉》)】:;.,?’):   
  26.             fix 1  
  27.             part = line[pos:pos+limit+fix]   
  28.     content += part + ‘\n’   
  29.     pos += limit + fix  

 

Python代码(2009-8-13更新)
  1. # -*- coding:gb2312 -*-   
  2. # python 2.5   
  3. # auther: moqi88 # gmail.com   
  4. # date: 2009-8-13   
  5. # you can get ASCII control code in http://www.cs.tut.fi/~jkorpela/chars/c0.html   
  6. import re   
  7. import time   
  8. import urllib   
  9. import telnetlib   
  10.   
  11. myfile = open(‘orange_weather.txt’, ‘r’)   
  12. myfile.read()   
  13. myfile.seek(0)   
  14. oldweather = myfile.read()   
  15. myfile.close()   
  16.   
  17. logfilename = time.strftime(‘orange_log%Y%m%d.txt’, time.localtime())   
  18. logfile         = open(logfilename, ‘a’)   
  19.   
  20. isfinish = False  
  21.   
  22. print time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 第 1 次尝试’   
  23. print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 第 1 次尝试’   
  24. page    = urllib.urlopen("http://www.hzqx.com/gzhfw/dqyb.asp")   
  25. u       = unicode(str(page.headers), ‘gb2312’)   
  26. data    = page.read()   
  27. match = re.search(ur"     (.*)96121", data)   
  28. if match == None:   
  29.     newweather = "今日天气暂无更新。更新更快的天气信息,请拨"  
  30. else:   
  31.     newweather = match.group(1)   
  32. newweather += "96121。"   
  33.   
  34. if (oldweather == newweather):   
  35.     print ‘信息源未更新’   
  36.     print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 信息源未更新’   
  37. else:   
  38.     f = file(‘orange_weather.txt’, ‘w’)   
  39.     f.write(newweather)   
  40.     f.close()   
  41.        
  42.     title   = time.strftime(‘%Y年%m月%d日%H时’, time.localtime())   
  43.     content = newweather  
  44.     content = re.sub(‘:’, ‘:’, content)   
  45.     content = re.sub(‘ ‘, ”, content)   
  46.        
  47.     pos       = 0  
  48.     limit   = 36  
  49.     line      = content.decode(‘gbk’)   
  50.     content =   
  51.        
  52.     while len(line) – pos > 0:   
  53.         fix = 0  
  54.         part = line[pos:pos+limit]   
  55.         part2 = part.encode(‘gbk’)   
  56.         while ((len(part2)<=(limit-2)*2) and (len(part)>=limit)):   
  57.             part = line[pos:pos+limit+fix]   
  58.             part2 = part.encode(‘gbk’)   
  59.             fix += 1   
  60.         else:   
  61.             if (len(part)*2==len(part2)):   
  62.                 part = part[:-1]   
  63.         content += part + ‘\n’   
  64.         pos += limit + fix – 1   
  65.        
  66.     user        = ‘username’  
  67.     passw   = ‘password’  
  68.     board   = ‘weather’  
  69.        
  70.     title     = time.strftime(‘%Y年%m月%d日%H时’, time.localtime())   
  71.        
  72.     server = telnetlib.Telnet(‘orangecity.3322.org’)   
  73.     time.sleep(5)   
  74.     server.write(user + ‘\n’)   
  75.     time.sleep(1)   
  76.     server.write(passw + ‘\n’)   
  77.     #data = server.read_very_eager()   
  78.     #data = re.sub("\x1B\[[0-9;]{0,}[mHJK]{1,1}", ”, data)   
  79.     #print data   
  80.     time.sleep(0.5)   
  81.     server.write(‘N\ntttttttN\n\nts’ + board + ‘\n’)   
  82.     time.sleep(0.5)   
  83.     server.write(‘\x10’ + title + ‘\n\n’)   
  84.     time.sleep(2)   
  85.     server.write(‘[‘+time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime())+’更新]\n’)   
  86.     time.sleep(2)   
  87.     server.write(content.encode(‘gbk’))   
  88.     time.sleep(2)   
  89.     server.write(‘\n\x17\n’)   
  90.     time.sleep(2)   
  91.     server.write(‘!\n’)   
  92.     server.close()   
  93.     print title   
  94.     print content.encode(‘gbk’)   
  95.     print ‘任务成功’   
  96.     print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 任务成功’   
  97.     isfinish = True  
  98.   
  99. if (isfinish == False):   
  100.     print ‘任务失败’   
  101.     print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 任务失败’   
  102. logfile.close()  

 

Python代码(2009-03-20版)
  1. # -*- coding:gb2312 -*-   
  2. # python 2.6   
  3. # auther: moqi88 # gmail.com   
  4. # date: 2009-3-20   
  5. # you can get ASCII control code in http://www.cs.tut.fi/~jkorpela/chars/c0.html   
  6. import re   
  7. import time   
  8. import urllib   
  9. import telnetlib   
  10.   
  11. server = telnetlib.Telnet(‘orangecity.3322.org’)   
  12. user   = ‘username’  
  13. passw  = ‘password’  
  14.   
  15. myfile = open(‘weather.txt’‘r’)   
  16. myfile.read()   
  17. myfile.seek(0)   
  18. oldweather = myfile.read()   
  19. myfile.close()   
  20.   
  21. logfilename = time.strftime(‘log%Y%m%d.txt’, time.localtime())   
  22. logfile     = open(logfilename, ‘a’)   
  23.   
  24. isfinish = False   
  25. for i in range(1, 16):   
  26.     print ‘time’ + str(i)   
  27.     print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 第 ‘ + str(i) + ‘ 次尝试’  
  28.     page  = urllib.urlopen("http://www.hzqx.com/gzhfw/dqyb.asp")   
  29.     u     = unicode(str(page.headers), ‘gb2312’)   
  30.     data  = page.read()   
  31.     match = re.search(ur"     (.*)96121", data)   
  32.     if match == None:   
  33.         newweather = "今日天气暂无更新。更新更快的天气信息,请拨"  
  34.     else:   
  35.         newweather = match.group(1)   
  36.     newweather += "96121。"  
  37.        
  38.     if (oldweather == newweather):   
  39.         print ‘信息源未更新’  
  40.         print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 信息源未更新’  
  41.     else:   
  42.         title = time.strftime(‘%Y年%m月%d日’, time.localtime())   
  43.         match = re.search(‘杭州市气象台(\d*)时’, newweather)   
  44.         if match == None:   
  45.             print ‘读取时间发布时间失败’  
  46.             print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 读取时间发布时间失败’  
  47.             title += time.strftime(‘%H时’, time.localtime())   
  48.         else:   
  49.             title += match.group(1)   
  50.             title += ‘时’  
  51.         content = newweather   
  52.         content = re.sub(‘:’‘:’, content)   
  53.         content = re.sub(‘ ‘, content)   
  54.         board   = ‘moqi’  
  55.            
  56.         time.sleep(5)   
  57.         server.write(user + ‘\n’ + passw + ‘\n’)   
  58.            
  59.         data  = server.read_until(user, 5)   
  60.         #data = re.sub("\x1B\[[0-9;]{0,}[mHJK]{1,1}", ”, data)   
  61.         server.write(‘Y\ntttttttN\n\nts’ + board + ‘\n\x10’ + title + ‘\n\n’ + content + ‘\x17\n’)   
  62.         time.sleep(2)   
  63.         server.write(‘!\n’)   
  64.         server.close()   
  65.   
  66.         print title   
  67.         print content   
  68.         print ‘任务成功’  
  69.         print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 任务成功’  
  70.         f = file(‘weather.txt’‘w’)   
  71.         f.write(newweather)   
  72.         f.close()   
  73.         isfinish = True   
  74.         break;   
  75.     time.sleep(60)   
  76.   
  77. if (isfinish == False):   
  78.     print ‘任务失败’  
  79.     print >> logfile, time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime()) + ‘ 任务失败’  
  80. logfile.close()