在新能源app开发的过程中,相关业务部门希望我能够爬取到各种新能源充电桩的信息后,能通过里面的经纬度,继续获取该充电桩周围的商圈情况,便于地图展示和用户体验的优化。我通过高德提供的api尝试抓取,下面是相关代码。但是,也依然会出现数据缺失率很高的问题。目前相关商圈信息还在研究和尝试中。如有解决方法,博主将继续更新。

import requests
import xlwt
import pandas as pd

#根据经纬度,逆向生成省市县
#高德地图
#key:去高德开发者平台申请,填你自己申请的key
def geocode(location):
    parameters = {'output': 'json', 'location': location, 'key': '',
                  'extensions': 'all','radius':'20'}
    base = 'https://restapi.amap.com/v3/geocode/regeo'
    response = requests.get(base, parameters)
    answer = response.json()
    #print('url:' + response.url)
    print(answer)
    return answer['regeocode']['addressComponent']['building']['name'], answer['regeocode']['addressComponent']['building']['type']


df1 = pd.read_excel("E:\cityNew\gps2.xlsx", usecols=["地址经度","地址纬度"], names=None)
df_li1 = df1.values.tolist()
polist = []
for i in range(len(df_li1)):
    xtmp = str(df_li1[i][0])
    ytmp = str(df_li1[i][1])

    locations = str(ytmp)+','+str(xtmp)
    #print(locations)
    name, type = geocode(locations)
    #print(province)
    #print(city)
    #print(district)

    if len(name) == 0 and len(type) == 0:
        polist.append('null' +','+'null')
    else:
        polist.append(str(name) + ',' + str(type))
print(polist)


def write_to_excel(polist):
    # 一个Workbook对象,这就相当于创建了一个Excel文件
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)

    # 第一行(列标题)
    sheet.write(0, 0, '地址名称')
    sheet.write(0, 1, '地址类型')


    for i in range(len(polist)):

        array = polist[i].split(",")
        sheet.write(i + 1, 0, array[0])
        sheet.write(i + 1, 1, array[1])
    book.save('所处位置.xls')

write_to_excel(polist)