利用python批量获取经纬度坐标(基于百度地图API)的代码

import requests
import time


def get_mercator(addr):
    url = 'http://api.map.baidu.com/geocoder/v2/?address=%s&output=json&ak=你的秘钥&callback=showLocation' % (addr)
    response = requests.get(url)
    return response.text


def writeline(src, dest):
    count = 0
    ms = open(src, encoding='utf-8')
    num = ms.readlines()
    print(len(num))
    print("-------------------")
    for line in num:
        with open(dest, "a", encoding='utf-8') as mon:
            loc = get_mercator(line)
            # lng_lat = eval(loc[27:-1])["result"]['location']
            # print(lng_lat)
            # mon.write(str(lng_lat))
            print(loc)
            mon.writelines(loc)
            mon.write("\n")
            time.sleep(0.1)
            count += 1
        print("第" + str(count) + "条数据写入成功...")
writeline(r"你的文件路径\address_test.txt", r"你的文件路径\address_results.txt")
  • 文件示例

利用python批量获取经纬度坐标(基于百度地图API)的代码_第1张图片

 

你可能感兴趣的:(python实验)