初学Python---------------天气预报
来源:互联网 发布:达内linux视频 百度云 编辑:程序博客网 时间:2024/06/05 10:56
今天利用百度的天气预报API,实现了一个可以查询全国城市天气预报的小脚本。亲测可用:
#!\usr\bin\python#encoding:utf-8import sysimport urllib2from xml.dom import minidomak = '6VmgXqokxQfh7tGFlPQKpLjQ'url = 'http://api.map.baidu.com/telematics/v3/weather?'def get_response(location):return urllib2.urlopen(url + 'location=' + location + '&ak=' + ak)def read_xml(xml):dom = minidom.parse(xml)return dom.documentElementdef show(node):if not node.hasChildNodes():if node.nodeType == node.TEXT_NODE and node.data != '\n':tag_name = node.parentNode.tagNamecontent = node.data.replace('\n','')if tag_name == 'currentCity' or tag_name == 'date' or tag_name == 'weather' or tag_name == 'wind':print contentif tag_name == 'temperature':print contentprint '---------------------------'else:for e in node.childNodes:show(e)if __name__ == '__main__':if len(sys.argv) != 2:print 'how to use:'print 'python weather.py location'sys.exit()else:location = sys.argv[1]root = read_xml(get_response(location))show(root)
0 0
- 初学Python---------------天气预报
- python天气预报
- python 天气预报
- Python 天气预报
- python 的查询天气预报
- python 实现天气预报功能
- Python读取天气预报
- Python查询天气预报
- python获取天气预报
- Python查询天气预报
- Python接入天气预报api
- python 天气预报获取
- 初学python
- 初学python
- 初学Python
- 初学python
- 初学python
- 初学Python
- 二维码扫描,中文解释源码
- MPEG-4视频压缩基础
- 2015上海华交会
- 怎样使用Gson 解析 (deserialize) json字符串
- C# 获取图片宽与高
- 初学Python---------------天气预报
- Linux FrameBuffer分析之编写基于FrameBuffer接口的应用程序
- 最长上升子序列
- 理解range()基本用法:
- 运行 Unix网络编程 中的第一个例子 时间日期例子
- python学习2
- rank() over
- Linux 查看cpu等
- wicket forward a url