夜勿眠 发表于 2017-5-8 12:32:40

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

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

  代码如下:
  #coding=utf-8import osimport reimport urllibimport emailimport mimetypesimport sysimport PyFetionimport sqlite3def sendFetion(PhoneNO, FetionPasswd, DestPhoneNO, Msg):try:phone = PyFetion.PyFetion(PhoneNO,FetionPasswd,"TCP")except PyFetion.PyFetionInfoError,e:print "corrent your mobile NO. and password"return -1phone.login()destPhoneList = DestPhoneNO.split(';')for destPhone in destPhoneList:phone.send_sms(Msg,destPhone, long=True)returndef 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+"'"cu.execute(sqltext)phones = cu.fetchall()data]=phonescu.close()cx.close()return dataif __name__ == "__main__":   phoneNO = ''fetionPasswd = ''phoneNO = "XXX"#此处为手机号fetionPasswd = "XXX"#此处为飞信密码try:data = GetData()for k in data:DestPhoneNO = datacity = 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+theDays+theWeathers+theWindsstrDest =";".join( for k in DestPhoneNO])print smscontent.decode("utf-8").encode("gb2312")print strDestsendFetion(phoneNO, fetionPasswd,strDest,smscontent)except:print "exception catched\n";   finally:print "The end of the executation! \tOh, \tHoly Shit!\n"
页: [1]
查看完整版本: python正则表达式分析新浪网天气预报,通过pyfetion发送短信的代码