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

本文介绍了一个使用Python编写的程序,该程序能够从新浪天气服务获取指定城市的天气预报信息,并通过飞信向预设的手机号码群发短信通知。程序涉及数据库操作以存储和检索用户信息,并使用了多种Python库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明

请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值