python 查天气
来源:互联网 发布:淘宝宝贝图片制作教程 编辑:程序博客网 时间:2024/06/07 16:34
这里我们借助中国天气网的三个接口,通过http get方式获取数据
获取省和直辖市的编号
http://m.weather.com.cn/data5/city.xml
例如
获取相应的二级城市编号
http://m.weather.com.cn/data5/city' + [省或直辖市编号] + '.xml
获取相应的地区编号
http://m.weather.com.cn/data5/city' + [二级城市编号]+ '.xml
最后将' 101' + [地区编号] 得到的就是可供查询的地区编号
查询相应地区的天气
http://www.weather.com.cn/data/cityinfo/[可供查询的地区编号].html
例如
希望能够输入城市就查询该城市或地区的天气,那么首先要获取完整的地区编号,这里通过get_city.py将所有地名对应的地区编号获取并写入到一个city.py文件中,并设置地名为键,对应的编号为值的dictionary结构。之后查询天气的模块导入city模块,并执行查询,完整代码如下
# -*- coding: utf-8 -*-import osimport urllib#根据url得到html代码def getHtml(url): page = urllib.urlopen(url) html = page.read() return html#讲字符串分割后整理为编号和对应城市的dictionarydef getDictionary(str): str_split = str.split(',') dics = {} for each in str_split: tmp = each.split('|') dics[tmp[0]] = tmp[1] return dics#得到编号和对应的省或直辖市dictionarydef getProvience(url): res = getHtml(url) pro_dic = getDictionary(res) return pro_dic#得到编号和对应的二级城市的dictionarydef getCity(url): res = getHtml(url) city_dic = getDictionary(res) return city_dic#得到编号和对应的区域的dictionarydef getField(url): res = getHtml(url) field_dic = getDictionary(res) return field_dicif __name__ == '__main__': #打开文件 city_code_file = open('city.py', 'w') city_code_file.write('# -*- coding: utf-8 -*-\n') city_code_file.write('city = {}\n') print '获取省以及直辖市编号' province_url = 'http://m.weather.com.cn/data5/city.xml' pro_dic = getProvience(province_url) #获取每个省的城市编号 print '获取二级区域编号' for pro in pro_dic: city_url = 'http://m.weather.com.cn/data5/city' + pro + '.xml' city_dic = getCity(city_url) #获取每个城市的地区编号 for city in city_dic: field_url = 'http://m.weather.com.cn/data5/city' + city + '.xml' field_dic = getField(field_url) #对于每一个编号和对应的地区存入dictionary for field in field_dic: city_code_file.write('city[\'' + field_dic[field] + '\'] = ' + '101' + str(field) + '\n') print 'city[\'' + field_dic[field].decode('utf-8') + '\'] = ' + '101' + str(field) #关闭文件 city_code_file.close() print '获取完成'
执行后将创建一个city.py文件,并按照python语法写入,使其查询时可直接作为模块导入,效果如下
查询天气
# -*- coding: utf-8 -*-import get_cityimport jsonimport cityfield_name = raw_input('哪里的天气?')field_no = city.city.get(field_name)if field_no:url = 'http://www.weather.com.cn/data/cityinfo/%s.html' % field_nohtml = get_city.getHtml(url)weather_dic = json.loads(html)weather_info = weather_dic.get('weatherinfo')print weather_info['weather'] + ' ' + weather_info['temp2'] + '~' + weather_info['temp1'] else:print '没有这个地名的天气信息'
0 0
- python 查天气
- 0905 python打开网页查天气~
- 用python写一段查天气的代码
- python基础教程共60课-第43课查天气1
- python基础教程共60课-第44课查天气2
- python基础教程共60课-第45课查天气3
- python基础教程共60课-第46课查天气3
- python获得天气
- python抓取天气
- 【Python】 天气查询
- python天气查询
- python网络天气爬虫
- python实现查询天气
- Python Flask 天气查询
- python查询天气
- Python 抓取中国天气网天气数据
- Python查询天气小程序
- 利用Python获取天气信息
- 【android】ListView的局部刷新
- C# 加密解密字符串
- C基础 - 运算符(5)
- 理解执行计划
- 利用友盟第三方分享
- python 查天气
- GDB调试
- 变量的 声明、存放与作用域
- exosip开发
- 常见SVN图标的含义
- 【教程】OpenCV3.0.0(Alpha)在VS2012下的配置
- 使用Java实现文件的操作
- mysql 数据库创建文档
- HDU1066 Last non-zero Digit in N!