1 Star 0 Fork 0

张一夫/BaiduMap_tiles

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
place_search.py 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
kyer 提交于 2018-02-28 14:45 . feat: upgrade python2 to python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 19 13:27:28 2016
@author: Min
"""
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
import json
import codecs
akey = 'uzIatbtHgfCTr71dWxnHolfZqG6vARNc'
def request_place_API(lat1,lng1,lat2,lng2,query,page):
bounds = lat1+","+lng1+","+lat2+","+lng2
url='http://api.map.baidu.com/place/v2/search'
args = {'query':query,
'page_size':10,
'page_num':page,
'bounds':bounds,
'output':'json',
'ak':akey}
data = urllib.parse.urlencode(args)
#req = urllib2.Request(url, data)
response = urllib.request.urlopen(url+'?'+data)
result = response.read()
#print result
return result
def request_place_API_all(lat1,lng1,lat2,lng2,query):
pre_result = request_place_API(lat1,lng1,lat2,lng2,query,0)
a = json.loads(pre_result)
tot = a["total"]
result_list = a["results"]
for i in range(1,tot//10+1):
i_result = request_place_API(lat1,lng1,lat2,lng2,query,i)
b = json.loads(i_result)
result_list += b["results"]
return result_list
def store_results(result,filename):
f = codecs.open(filename,'w','utf-8')
for block in result:
f.write(block['name']+"\n")
lat = block['location']['lat']
lng = block['location']['lng']
f.write(str(lat)+' '+str(lng)+'\n')
#f.write(unicode(block[u'location'],'utf-8'))
f.close()
def clean_list(a):
b = set()
result = []
for block in a:
name = block['name']
if name not in b:
b.add(name)
result.append(block)
return result
if __name__ == "__main__":
lat1 = '31.022547'
lng1 = '121.429391'
lat2 = '31.041453'
lng2 = '121.45749'
#query = "楼"
blocks_in_SJTU = [];
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"楼")
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"中心")
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"馆")
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"宿舍")
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"学生公寓")
blocks_in_SJTU += request_place_API_all(lat1,lng1,lat2,lng2,"院")
blocks_in_SJTU = clean_list(blocks_in_SJTU)
filename = "result.txt"
store_results(blocks_in_SJTU, filename)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Dontes_cn/BaiduMap_tiles.git
git@gitee.com:Dontes_cn/BaiduMap_tiles.git
Dontes_cn
BaiduMap_tiles
BaiduMap_tiles
master

搜索帮助