指定地区天气预报查询的Python方法,地区天气预报python,偶然间看到一段指定地区天
指定地区天气预报查询的Python方法,地区天气预报python,偶然间看到一段指定地区天
偶然间看到一段指定地区天气预报查询的Python方法感觉功能不错,贴出来和大家分享一下。python天气预报的代码虽然不是很长,但看上去不太好理解,因为用到了python re正则表达式及urllib和sys方法类库的应用。
指定地区天气预报查询的Python方法源码如下:
(代码仅供参考,实际应用时请适当修改)
#!/usr/bin/env python#coding=utf-8import urllib ,sysimport reprovice=raw_input('输入省名(请使用拼音):');major=raw_input("输入市名(请使用拼音):")url="http://qq.ip138.com/weather/"+provice+'/'+major+'.htm'print url#url="http://www.baidu.com"#www.iplaypy.comwetherhtml=urllib.urlopen(url)result=wetherhtml.read().decode('GB2312')#.encode('utf-8')#result=result.replace("gb2312","utf-8")f=file('weather.txt','w')f.write(result.encode('GB2312'))f.close()pattern='Title.+<b>(.+)</b>'Title=re.search(pattern,result).group(1)pattern='>(\d*-\d*-\d*.+?)<'date=re.findall(pattern,result)pattern='alt="(.+?)"'weather=re.findall(pattern,result)pattern='<td>([-]?\d{1,2}.+)</td>'temperature=re.findall(pattern,result)print "%35.30s"%Title,""length=len(date)for i in range(length):print '%30.20s'%date[i],'\t%s'%weather[i],'\t%s'%temperature[i]
Python天气预报相关文章推荐:一个用Python编写抓取天气预报的代码示例
编橙之家文章,
相关内容
- Python识别网站验证码的方法源码,,学习Python教程之前
- Python压缩文件为tar、gzip的方法,pythontar,Python文件操作
- Python 匹配手机电话号码及邮箱的正则表达式源码,py
- Python POP3协议收取邮件方法,pythonpop3协议收取,Python教程
- Python实现DES加密算法源码例详解,pythondes,本篇为大家提
- 批量下载google图片的Pytho方法源码,,Python教程学习,本
- Python socket方法查询whois协议脚本源码,whois脚本源码,
- Python免费SSH帐号获取器源码,pythonssh帐号源码,Python免费
- Python调用短信猫控件实现自动发短信功能,python控件
- Python解析xml文本的实现方法,python解析xml文本,编橙之家
评论关闭