python写脚本查询地址所在街道

媳妇儿她们公司承接普查,需要对地址进行街道的划分,故写了这个脚本帮助她搞定这些, 思索:如果不知道高德的开发者API,也不会写脚本,几千条数据的街道,光靠着web版的地图挨个查,数街道,估计得通宵也搞不定,政府部门的电子化任重而道远...到现在连个正经ERP系统都没有,街道划分图拿给她一张5年前的手绘图...非常厉害了! 需求: 利用地图搜索地址信息,查询地址所在街道,然后记录
实现如下

  • 利用pandas读取csv文件里的内容
  • 调用高德的web api接口查询地理编码信息,由于正向地理编码信息,很少有地址所在街道(township),所以取得其经纬度坐标,然后逆向地理编码取得所在街道(township)
  • 然后再用pandas输出生成excel
pandas使用操作:
pip install pandas 如果报 [PermissionError: [Errno 13] Permission denied: '/Users/yangda/.virtualenv/educational *venv*/lib/python3.6/site-packages/__pycache__/six.cpython-36.pyc'](https://www.google.com/search?biw=1280&bih=680&q=PermissionError:+%5BErrno+13%5D+Permission+denied:+%27/Users/yangda/.virtualenv/educational+venv/lib/python3.6/site-packages/__pycache__/six.cpython-36.pyc%27&spell=1&sa=X&ved=0ahUKEwiLgb7GsevaAhVGxGMKHThzD2sQBQgjKAA) 使用 sudo pip install pandas

pandas.read_csv参数详解
实现代码:
# -*- coding:utf-8 -*- from __future__ import unicode_literals__author__ = 'yang_da' __date__ = '2018/5/4 下午1:44'import requests import pandas as pd import time import sysdef parse(): total_data = https://www.it610.com/article/pd.read_csv('begin.csv', header=1) total_list_data = https://www.it610.com/article/total_data['123456'] return total_list_datadef search_location(address): params = {'address': address, 'city': '北京', 'key': '请写入你自己的key'} base_url = 'http://restapi.amap.com/v3/geocode/geo' response = requests.get(url=base_url, params=params) json_data = https://www.it610.com/article/response.json() if json_data['status'] == '1' and len(json_data['geocodes']) > 0: return json_data['geocodes'][0]['location'] if json_data['geocodes'][0]['location'] else 'errorlocation', json_data return 'errorlocation', json_datadef search_address_detail(location): params = {'location': location, 'key': '写入你的key'} base_url = 'http://restapi.amap.com/v3/geocode/regeo' response = requests.get(url=base_url, params=params) json_data = https://www.it610.com/article/response.json() if json_data['status'] == '1': if json_data['regeocode']['addressComponent']['township']: return json_data['regeocode']['addressComponent']['township'], json_data return 'errorSearchAddress', json_dataif __name__ == '__main__': df = pd.DataFrame(columns=['originAddress', 'location', 'anotherInfo', 'searchTownship', 'searchAnotherAddress']) address_list = parse() index = 0 for address in address_list: # 地址,处理坑爹数据 address_deal = address.split(' ',1) real_address = '' for real in address_deal: if len(real) > len(real_address): real_address = real # 搜索location location_str, search_json = search_location(address=real_address) # 利用location搜索township township_str, search_address_json = search_address_detail(location=location_str) df.loc[index] = [address, location_str, search_json, township_str, search_address_json] index = index + 1 print('正在查询') print(index) df.to_csv('locationDetail.csv', index=True) print('输出csv成功')

【python写脚本查询地址所在街道】参考:
pandas入门
python调高德api

    推荐阅读