今天周末,正好昨天对python的xml处理稍微了解了下,所以就拿出前几天申请的一个百度map的key,来处理下返回的xml文件,还好对着别人的做法,依葫芦画瓢,搞出来了.感觉效率不怎么高,估计是处理的方法有问题.凑合用了.
#!/usr/bin/python
#encoding=utf8
import urllib,sys,xml.dom.minidom
from xml.dom.minidom import Node
def baidu_map(query,region):
params=urllib.urlencode({'query':query,'region':region,'output':"xml",'key':"aba6d4d0009d55b95162dc1f107e58a3"})
f=urllib.urlopen("http://api.map.baidu.com/place/search?&%s"% params)
return f
def readxml(filename):
doc=xml.dom.minidom.parse(filename)
for node in doc.getElementsByTagName("result"):
name=None
address=None
lat=None
lng=None
telephone=None
NAME=node.getElementsByTagName('name')
for node2 in NAME:
for node3 in node2.childNodes:
if node3.nodeType==Node.TEXT_NODE:
name = node3.data
ADDRESS=node.getElementsByTagName('address')
for node2 in ADDRESS:
for node3 in node2.childNodes:
if node3.nodeType==Node.TEXT_NODE:
address = node3.data
TEL=node.getElementsByTagName('telephone')
for node2 in TEL:
for node3 in node2.childNodes:
if node3.nodeType==Node.TEXT_NODE:
telephone = node3.data
LOC=node.getElementsByTagName('location')
for node2 in LOC:
for node3 in node.getElementsByTagName('lat'):
for node4 in node3.childNodes:
if node4.nodeType==Node.TEXT_NODE:
lat =node4.data
for node3 in node.getElementsByTagName('lng'):
for node4 in node3.childNodes:
if node4.nodeType==Node.TEXT_NODE:
lng =node4.data
print "name=%s,address=%s,TEL=%s,location=%s,%s"%(name,address,telephone,lat,lng)
if __name__=="__main__":
if(len(sys.argv)==3):
query=sys.argv[1]
region=sys.argv[2]
result=baidu_map(query,region)
fd=open('result','wb')
fd.write(result.read())
fd.close()
else:
print "参数不对"
readxml('result')
[averainy@localhost py]$ ./baidu_map.py 公园 深圳
name=中山公园,address=南山区中山西街42号,TEL=(0755)26503722,location=22.550798,113.921603
name=海滨公园,address=广东省深圳市宝安区,TEL=None,location=22.593337,113.851477
name=荔枝公园,address=红岭中路1001号,TEL=(0755)82095655,location=22.552848,114.110802
name=洪湖公园,address=文锦北路2023号,TEL=(0755)25613536,location=22.574921,114.129083
name=中山公园,address=南山区中山西街(汇泉广场对面),TEL=(0755)26503722,location=22.555078,113.929433
name=荔枝公园,address=太子路与荔园路交汇处,TEL=None,location=22.493819,113.923314
name=市民广场,address=上德路,TEL=None,location=22.732243,113.837747
name=中心公园,address=广东省深圳市宝安区,TEL=None,location=22.688687,114.069675
name=新城广场,address=宝安宝安西乡流塘路与107国道交界处,TEL=None,location=22.586549,113.889695
name=皇岗双拥公园,address=广东省深圳市福田区,TEL=None,location=22.52382,114.064393
name=龙园,address=广东省深圳市龙岗区,TEL=None,location=22.738963,114.272036
name=龙园,address=龙园路416对面,TEL=(0755)28838044,location=22.738258,114.274237
name=新城广场,address=埔夏路86号,TEL=None,location=22.650742,114.192648
name=凤凰山森林公园,address=广东省深圳市宝安区,TEL=None,location=22.679899,113.841756
name=东湖公园,address=广东省深圳市罗湖区,TEL=None,location=22.571071,114.15545
name=中心公园,address=广东省深圳市福田区,TEL=None,location=22.54884,114.085817
name=南山公园,address=沿山路,TEL=None,location=22.502849,113.924409
name=笔架山公园,address=皇岗路,TEL=(0755)83320237, (0755)83246662,location=22.569103,114.086721
name=南山公园,address=广东省深圳市南山区,TEL=None,location=22.499407,113.909333
name=坪山公园,address=坪山汽车总站斜对面,TEL=None,location=22.693567,114.34939