Python模拟Web Fetion给好友发送信息(HTML解析和具体发送模块)
来源:互联网 发布:js unicode编码转换 编辑:程序博客网 时间:2024/06/01 10:31
这部分代码有些地方就直接用了我同学Pzjay的代码,也是他的代码给我带来写这个小程序的思路,对此表示感谢。
这里就是主要用到BeautifulSoup包来解析HTML的
还是直接来代码:
#-*-coding:utf-8-*-from BeautifulSoup import BeautifulSoupimport Fetionimport urllib2,os,sysimport dbOperclass WeatherUser: def __init__(self,name,province,city,phoneId): self.name = name self.province = province self.city = city self.phoneId = phoneIdclass 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()
- Python模拟Web Fetion给好友发送信息(HTML解析和具体发送模块)
- Python模拟Web Fetion给好友发送信息(HTML解析和具体发送模块)
- Python模拟Web Fetion给好友发送信息
- Python模拟Web Fetion给好友发送信息(数据库编程模块)
- Python模拟Web Fetion登录解析
- 模拟微信发送文件给好友/群
- 新技能,微信批量发送中秋节信息给好友~~
- 微信开发--分享到朋友圈和发送给好友
- Web邮件发送客户端 [ html + php + python ]
- itchat 批量给好友发送祝福消息
- 通过itchat进行给好友发送消息
- 发送HTML信息
- Python发送邮件模块
- python学习笔记------发送邮件(文本和html)
- python获取天气信息,给指定用户发送邮件
- Python向飞信好友发送短信
- Android发送信息模拟系统
- PHP模拟发送POST请求之三、用Telnet和fsockopen()模拟发送POST信息
- 黑马程序员训练营--浅谈c#中值传递和引用传递
- sqlserver查看缓存中数据页面和索引页面
- gcc警告选项
- sshd配置
- object-c的委托模式
- Python模拟Web Fetion给好友发送信息(HTML解析和具体发送模块)
- 12、J2SE基础-字符编码
- 愿景派架构师
- Android短信Receiver优先级
- [wp7软件]wp7彩票软件系列~~~集合贴~~~
- 设计模式(一)——重新认识面向对象
- OGNL表达式struts2标签“%,#,$”
- [转]Redis消息通知系统的实现
- onload事件-----addLoadEvent函数