目录
1.返回地址经纬度与地址类型
转载: https://blog.csdn.net/xxzj_zz2017/article/details/79396980
import pandas as pd
import json
from urllib.request import urlopen, quote
import csv
import traceback
import os
# 构造获取经纬度的函数
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoder/v2/?address='
output = 'json'
ak=r'ytWXMjPN6kShzvRuI4yBxoiZOnDGe3LG'
# ak = '你的ak'#需填入自己申请应用后生成的ak
add = quote(address) # 本文城市变量为中文,为防止乱码,先用quote进行编码
url2 = url + add + '&output=' + output + "&ak=" + ak
req = urlopen(url2)
res = req.read().decode()
temp = json.loads(res)
return temp
"""
file = open('经纬度.json', 'w') # 建立json数据文件
data_1 = pd.read_csv("house_price.csv") # 读取小区房价信息
for i in data_1.values:
try:
b = i[0].strip()
c = str(i[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)
except:
f = open("异常日志.txt", 'a')
traceback.print_exc(file=f)
f.flush()
f.close()
file.close()
"""
print(getlnglat("电子烟"))
2.获取商家店名地址电话
转载:https://blog.csdn.net/qq_42646885/article/details/81214783
import requests
"""
# 百度地图API搜索
def baidu_search(query, region):
url = 'http://api.map.baidu.com/place/v2/search?'
output = 'json'
ak = 'ytWXMjPN6kShzvRuI4yBxoiZOnDGe3LG'
uri = url + 'query=' + query + '®ion=' + region + '&output=' + output + '&ak=' + ak
r = requests.get(uri)
response_dict = r.json()
results = response_dict["results"]
for adr in results:
name = adr['name']
location = adr['location']
lng = float(location['lng'])
lat = float(location['lat'])
address = adr['address']
telephone = adr['telephone']
print('名称:' + name)
print('坐标:%f,%f' % (lat, lng))
print('地址:' + address)
print('电话:' + telephone)
"""
#print(baidu_search('海底捞', '广州'))
query=r'电子烟'
region='广州'
url = 'http://api.map.baidu.com/place/v2/search?'
output = 'json'
ak = 'ytWXMjPN6kShzvRuI4yBxoiZOnDGe3LG'
uri = url + 'query=' + query + '®ion=' + region + '&output=' + output + '&ak=' + ak
r = requests.get(uri)
response_dict = r.json()
results = response_dict["results"]
for adr in results:
print(adr)