import requests
import json
import pandas as pd
df = pd.read_csv('101618freq.csv',low_memory= False, dtype=object,encoding='gbk')
# 构造请求
req_geo_url = ''
req_geo_s = 'https://restapi.amap.com/v3/geocode/geo?address='
req_geo_e = '&key=e633ff2494c6ad96864c5b8a0e29d9d6'
req_regeo_url = ''
req_regeo_s = 'https://restapi.amap.com/v3/geocode/regeo?output=xml&location='
req_regeo_e = '&key=e633ff2494c6ad96864c5b8a0e29d9d6&radius=1000&extensions=all'
headers = {
'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; Media Center PC 6.0)',
}
list_err_url = [] # 存储错误的url
# 对标题进行重新排序,默认取第3列为地址,并追加后续列,如果已经有,则会保存。reIndex需要通过返回赋值,没有inplace参数。
new_columns = [df.columns[1]] + ['执行结果','标准地址','国家','省份','城市','县区代码','县区','乡镇代码','乡镇','街道',"乡村地址"]
df = df.reindex(columns=new_columns)
# 请求函数
def append_address(x):
url = req_geo_s + str(x[0]) + req_geo_e
print('执行序号:',str(x.name),'地址:',str(x[0]),'url:',url)
try:
r = requests.get(url,timeout=5,headers = headers) # 设置访问超时,以及http头
r_json = json.loads(r.text)
formatted_address = r_json["geocodes"][0]["formatted_address"]
country = r_json["geocodes"][0]["country"]
province = r_json["geocodes"][0]["province"]
city = r_json["geocodes"][0]["city"]
district = r_json["geocodes"][0]["district"]
township = r_json["geocodes"][0]["township"]
lat_long = r_json["geocodes"][0]["location"]
level = r_json["geocodes"][0]["level"]
return(formatted_address,country,province,city,district,township,lat_long,level)
except:
print('error url:',url)
df_sel = df['执行结果'] != 1
df.loc[df_sel,"执行结果"],df.loc[df_sel,"标准地址"],df.loc[df_sel,"国家"],df.loc[df_sel,"省份"],df.loc[df_sel,"城市"],df.loc[df_sel,"县区代码"],df.loc[df_sel,"县区"],df.loc[df_sel,"乡镇代码"],df.loc[df_sel,"乡镇"],df.loc[df_sel,"街道"] = zip(*df[df_sel].apply(append_address, axis=1))
|