python写脚本查询地址所在街道
媳妇儿她们公司承接普查,需要对地址进行街道的划分,故写了这个脚本帮助她搞定这些,
思索:如果不知道高德的开发者API,也不会写脚本,几千条数据的街道,光靠着web版的地图挨个查,数街道,估计得通宵也搞不定,政府部门的电子化任重而道远...到现在连个正经ERP系统都没有,街道划分图拿给她一张5年前的手绘图...非常厉害了!
需求: 利用地图搜索地址信息,查询地址所在街道,然后记录
实现如下
- 利用pandas读取csv文件里的内容
- 调用高德的web api接口查询地理编码信息,由于正向地理编码信息,很少有地址所在街道(township),所以取得其经纬度坐标,然后逆向地理编码取得所在街道(township)
- 然后再用pandas输出生成excel
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
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 今天写一些什么
- python学习之|python学习之 实现QQ自动发送消息
- 小影写在2018九月开学季
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- 写给陈羡
- angular2内置管道