代码拉取完成,页面将自动刷新
# -*- 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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。