首先,感谢百度给提供的接口
以下是关于基于百度地图拾取的地址转坐标的转换方式,核心数据获url:
http://api.map.baidu.com/geocoder/v2/?output=json&ak={ak}&address={address}
然后,申请属于自己的ak
http://lbsyun.baidu.com/
最后附上代码
mport requests
import time
class get_GPS:
def __init__(self,ak='申请到的ak'):
self.ak = ak
def get_local(self,address):
url = f"""http://api.map.baidu.com/geocoder/v2/?output=json&ak={self.ak}&address={address}"""
req = requests.get(url)
result = eval(req.content.decode('utf-8'))
print(result)
lng = result['result']['location']['lng']#经度
lat = result['result']['location']['lat']#纬度
return lng,lat
def read_org_name(self):
self.address_list = [ [j for j in i.strip().replace("\"",'').split(',')] for n,i in enumerate(open('xxx.csv','r',encoding = 'utf-8')) if n>0 ]
def get_org_name_GPS(self):
self.read_org_name()
for [address] in self.address_list:
lng, lat = self.get_local(address=address)
with open('输出文件名.txt', 'a') as f:
f.write(f"""{address},{lng},{lat}\n""")
time.sleep(10)
if __name__ == "__main__":
T = get_GPS()
T.get_org_name_GPS()