import json
from urllib.request import urlopen, quote
import requests,csv
import pandas as pd
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoder/v2/'
output = 'json'
ak = '你申请的密钥***'
add = quote(address)
uri = url + '?' + 'address=' + add + '&output=' + output + '&ak=' + ak
req = urlopen(uri)
res = req.read().decode()
temp = json.loads(res)
return temp
file = open(r'E:\\爬虫数据分析\调用百度地图api\point.json','w')
with open(r'E:\\爬虫数据分析\调用百度地图api\各区域房价.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
for line in reader:
if reader.line_num == 1:
continue
b = line[0].strip()
c= line[1].strip()
lng = getlnglat(b)['result']['location']['lng']
lat = getlnglat(b)['result']['location']['lat']
str_temp = '{"lat":' + str(lat) + ',"lng":' + str(lng) + ',"count":' + str(c) +'},'
file.write(str_temp)
file.close()