python爬虫百度地图_用python解析处理百度地图的xml文件

今天周末,正好昨天对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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值