python正则表达式分析新浪网天气预报,通过pyfetion发送短信的代码

 

版权声明

请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正。

 

代码如下:

 #coding=utf-8 import os import re import urllib import email import mimetypes import sys import PyFetion import sqlite3 def sendFetion(PhoneNO, FetionPasswd, DestPhoneNO, Msg): try: phone = PyFetion.PyFetion(PhoneNO,FetionPasswd,"TCP") except PyFetion.PyFetionInfoError,e: print "corrent your mobile NO. and password" return -1 phone.login() destPhoneList = DestPhoneNO.split(';') for destPhone in destPhoneList: phone.send_sms(Msg,destPhone, long=True) return def GetData(): cx = sqlite3.connect("d://TVGuide//sms.db") cu = cx.cursor() cu.execute("select distinct(city) from data") citys = cu.fetchall() data = {} for city in citys: sqltext = "select phoneNo from data where city='"+city[0]+"'" cu.execute(sqltext) phones = cu.fetchall() data[city[0]]=phones cu.close() cx.close() return data if __name__ == "__main__": phoneNO = '' fetionPasswd = '' phoneNO = "XXX"#此处为手机号 fetionPasswd = "XXX"#此处为飞信密码 try: data = GetData() for k in data: DestPhoneNO = data[k] city = urllib.urlencode({"city":k.encode('gb2312')}) sock = urllib.urlopen("http://php.weather.sina.com.cn/search.php?f=1&"+city+"&dpc=1") strhtml = sock.read() strhtml = unicode(strhtml, 'gb2312','ignore').encode('utf-8','ignore') theDates = re.findall('''<p>(/d+.*?)</p>''', strhtml) theDates = map(lambda x:x.replace(" "," "), theDates) theDays = re.findall('''<h3>(.*?)</h3>''', strhtml) theWeathers = re.findall('''<div class="Weather_TP">(.*?)</div>''', strhtml) theWinds = re.findall('''<div class="Weather_W">(.*?)</div>''', strhtml) theWinds = map(lambda x:x.replace(" "," "), theWinds) smscontent =""; for i in range(0,3): smscontent =smscontent + theDates[i]+theDays[i]+theWeathers[i]+theWinds[i] strDest =";".join([k[0] for k in DestPhoneNO]) print smscontent.decode("utf-8").encode("gb2312") print strDest sendFetion(phoneNO, fetionPasswd, strDest,smscontent) except: print "exception catched/n"; finally: print "The end of the executation! /tOh, /tHoly Shit!/n"

你可能感兴趣的:(exception,正则表达式,python,sqlite,Class,lambda)