#encoding=utf-8
import json
import pprint
import re
import os
import pandas as pd
import requests
import time
def getAddressLocation(addr_keyword,store_name,repeat_sign=False):
''' repeat_sign: 用于第一次请求数据没请求到,第二次经过处理后的请求,如果第二次请求失败了,就不继续请求了,防止无限制循环'''
# 伪返回地址信息处理,用于后面接口填写的实际地址,可能不是高德查出来的地址
addr_keyword_addr_data = addr_keyword
addr_keyword_provice_list = re.findall("(.*?省).*",addr_keyword)
addr_keyword_city_list = re.findall(".*省(.*?市).*",addr_keyword)
addr_keyword_district_list = re.findall(".*市(.*?区).*",addr_keyword)
if addr_keyword_provice_list:
addr_keyword_addr_data = addr_keyword_addr_data.lstrip(addr_keyword_provice_list[0])
if addr_keyword_city_list:
addr_keyword_addr_data = addr_keyword_addr_data.lstrip(addr_keyword_city_list[0])
if addr_keyword_district_list:
if len(addr_keyword_district_list[0])<=6:
addr_keyword_addr_data = addr_keyword_addr_data.lstrip(addr_keyword_district_list[0])
print(addr_keyword,"--",addr_keyword_addr_data)
#高德定位查询
host = "https://restapi.amap.com/"
path = "v3/place/text"
url = host + path
# 定位请求数据
body = {
"s": "rsv3",
"children":None,
"key": "8325164e247e***68b59e892****",
"page": "1",
"offset": "10",
"city": "330100",
"language": "zh_cn",
"callback": "jsonp_175563_",
"platform": "JS",
"logversion": "2.0",
"sdkversion": "1.3",
"appname": "https://lbs.amap.com/console/show/picker",
"csid": "BC47D9A3-****-****-8440-C7D6300732B0",
"keywords": addr_keyword,
}
data = requests.get(url,body,verify=r'C:\Users\jiangbo.xu\Desktop\tp.pem')
headers = {
"Referer": "https://lbs.amap.com/console/show/picker",
"Sec-Fetch-Mode": "no-cors",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
}
response_data = data.content.decode("utf-8")
# print(response_data)
# print(type(response_data))
analysis_response_data_list = re.findall(".*_\((.*)\)",response_data)
analysis_response_data = json.loads(analysis_response_data_list[0])
# print(analysis_response_data)
# print(analysis_response_data["pois"][0])
if analysis_response_data["info"].lower() == "ok":
if len(analysis_response_data["pois"]):
addr_data = analysis_response_data["pois"][0]
elif analysis_response_data.get("sug_address"):
addr_data = analysis_response_data["sug_address"]
else:
if not repeat_sign:
addr_keyword_replace_address_list = re.findall("(.*路).*", addr_keyword)
addr_keyword_replace_adname_list = re.findall("(.*区).*", addr_keyword)
addr_keyword_replace_cityname_list = re.findall("(.*市).*", addr_keyword)
addr_keyword_replace_pname_list = re.findall("(.*省).*", addr_keyword)
addr_keyword_replace_list = []
addr_keyword_replace_list.append(addr_keyword_replace_address_list)
addr_keyword_replace_list.append(addr_keyword_replace_adname_list)
addr_keyword_replace_list.append(addr_keyword_replace_cityname_list)
addr_keyword_replace_list.append(addr_keyword_replace_pname_list)
for addr_keyword_index in addr_keyword_replace_list:
if len(addr_keyword_index):
addr_keyword_replace = addr_keyword_index[0]
pname, cityname, adname, addr_keyword_replace, location_data,store_name = getAddressLocation(
addr_keyword_replace, store_name,True)
return pname, cityname, adname, addr_keyword_addr_data, location_data,store_name
else:
print("已经重复查询一次,但是还是未查出数据,退出")
os._exit(0)
# print(pname," ",cityname," ",adname," ",address," ",name," ",location_data)
pname = addr_data["pname"]
cityname = addr_data["cityname"]
adname = addr_data["adname"]
if isinstance(adname,list):
adname = cityname
print("区转换市")
address = addr_data["address"]
name = addr_data["name"]
location_data = addr_data["location"]
return pname,cityname,adname,addr_keyword_addr_data,location_data,store_name
else:
if not repeat_sign:
addr_keyword_replace_address_list = re.findall("(.*路).*", addr_keyword)
addr_keyword_replace_adname_list = re.findall("(.*区).*", addr_keyword)
addr_keyword_replace_cityname_list = re.findall("(.*市).*", addr_keyword)
addr_keyword_replace_pname_list = re.findall("(.*省).*", addr_keyword)
addr_keyword_replace_list = []
addr_keyword_replace_list.append(addr_keyword_replace_address_list)
addr_keyword_replace_list.append(addr_keyword_replace_adname_list)
addr_keyword_replace_list.append(addr_keyword_replace_cityname_list)
addr_keyword_replace_list.append(addr_keyword_replace_pname_list)
for addr_keyword_index in addr_keyword_replace_list:
if len(addr_keyword_index):
addr_keyword_replace = addr_keyword_index[0]
pname, cityname, adname, addr_keyword_replace, location_data,store_name = getAddressLocation(addr_keyword_replace,store_name,True)
return pname, cityname, adname, addr_keyword_addr_data, location_data,store_name
else:
print("已经重复查询一次,但是还是未查出数据,退出")
os._exit(0)
def readExcelStoreAddress(file):
df = pd.read_excel(file)
#print(df.loc[df.index[0]])
data_list = []
for i in df.index:
addr_data = df.loc[i,"收件详细地址"]
store_name = df.loc[i,"收件公司"]
data = getAddressLocation(addr_data,store_name)
time.sleep(0.5)
print(data)
data_list.append(data)
return data_list
if __name__ == "__main__":
#file = "E:\\xujb\\test.xlsx"
file = "E:\\xujb\\shh\\22.xlsx"
readExcelStoreAddress(file)
# addr_data = "横峰县人民政府"
# data = getAddressLocation(addr_data)
# print(data)