这部分代码有些地方就直接用了我同学Pzjay的代码,也是他的代码给我带来写这个小程序的思路,对此表示感谢。
这里就是主要用到BeautifulSoup包来解析HTML的
还是直接来代码:
#-*-coding:utf-8-*- from BeautifulSoup import BeautifulSoup import Fetion import urllib2,os,sys import dbOper class WeatherUser: def __init__(self,name,province,city,phoneId): self.name = name self.province = province self.city = city self.phoneId = phoneId class WeatherReport: def __init__(self,weather_url = "http://www.nmc.gov.cn/publish/forecast/"): self.url = weather_url self.users = [] def _loadDbOper(self):#连接数据库 self._sqlite = "sqlite:///weather.sqlite" self._db = dbOper.DbOper(self._sqlite)#得到数据库类,即得到一个接口 def _loadProvinceMap(self):#得到ProvincesMap res = self._db.queryProvinceMap() self.provinceMap = {} for i in xrange(0,len(res)): key = res[i][0] value = res[i][1] self.provinceMap[key] = value #print key,value def _loadCityMap(self): self.cityMap = {} res = self._db.queryCityMap() for i in xrange(0,len(res)): key = res[i][0].encode('gbk') value = res[i][1] self.cityMap[key] = value #print key def _getUsers(self):#得到所有用户信息 res = self._db.queryFetionUsers() for i in xrange(0,len(res)): username = res[i][0].encode('gbk') province = res[i][1].encode('gbk') city = res[i][2].encode('gbk') id = res[i][3] self.users.append(WeatherUser(username,province,city,id)) def _loginFetion(self):#登录飞信 phoneId = '151********'#你的手机号码 phonePsd = '********'#你的飞信密码 phoneUrl = 'http://f.10086.cn/im5/login/login.action' self.__myPhone = Fetion.PyFetion(phoneId,phonePsd,phoneUrl) bLogin = self.__myPhone.fetionLogin() if bLogin: print "登录成功" else: print "登录失败" def _logoutFetion(self): self.__myPhone.fetionLogout() def parseDate(self, weather_div):#解析 date = weather_div.find('div', {'class': 'name'}).contents[0] week = weather_div.find('div', {'class': 'name'}).contents[2] return date + ' ' + week + ' ' def parseWeather(self, weather):#解析 lis = weather('li', limit=10) day = lis[0].string dtempurature = lis[2].next drain = lis[3].string dwind = lis[4].string night = lis[5].string ntempurature = lis[7].next nrain = lis[8].string nwind = lis[9].string return day + ': ' + dtempurature + ' ' + drain + ' '\ + dwind + '; ' + night + ': ' + ntempurature + ' ' + nrain + ' ' + nwind def _sendMsg(self,release_time,weather_info): msg1 = self.parseDate(weather_info) msg2 = self.parseWeather(weather_info) msg = msg1 + msg2 + '\n' return msg def _send_SMS(self,msg_title,msg_day1,msg_day2,user):#发送信息 msg = msg_title + msg_day1 + msg_day2 + '系统测试中,收到请回复下哦,亲' print msg self.__myPhone.sendMsg(msg.encode('utf-8'),user.phoneId)#发送 self._db.insertSendMsgInfo(user.name.encode('utf-8'),msg)#存储到数据库中 def _getWeatherInfo(self): for user in self.users:#对每个用户都发送 url = self.url + self.provinceMap[unicode(user.province)] + '/' + self.cityMap[user.city] + '.html' #得到中央气象台具体的URL #print url page = urllib2.urlopen(url).read().decode('gbk').encode('utf-8') soup = BeautifulSoup(page)#得到网页信息 city_body = soup.find('div', {'class': 'w365border city_body'}) weather_info = city_body.findAll('div', {'class': 'weather_div'}) title_bar = soup.find('div',{'class':'title_bar'}) weather_title = title_bar.find('div',{'class':'weather_title'}) release_time = weather_title.contents[0] msg_title = '尊敬的 【'+user.name+'】 用户,中央气象台'+release_time+'\n【' + user.city + '】的明后两天的天气情况:\n' msg_day1 = self._sendMsg(release_time,weather_info[1]) msg_day2 = self._sendMsg(release_time,weather_info[2]) self._send_SMS(msg_title,msg_day1,msg_day2, user) def weatherRun(self): self._loadDbOper() self._loadProvinceMap() self._loadCityMap() self._getUsers() self._loginFetion() self._getWeatherInfo() self._logoutFetion() if __name__ == "__main__": weather = WeatherReport() weather.weatherRun()