You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
1 year ago
|
# -*- encoding: utf-8 -*-
|
||
|
# 查询城市的经纬度并存入excel中
|
||
|
# 本次整体的源代码
|
||
|
AK = "9AfjsK2CXGuESwSb4sF3LsGhWh999Rdd"
|
||
|
|
||
|
import pandas as pd
|
||
|
import requests
|
||
|
import json
|
||
|
|
||
|
def getPosition(address):
|
||
|
url = r"http://api.map.baidu.com/place/v2/search?query={}®ion=全国&output=json&ak={}".format(
|
||
|
address,
|
||
|
AK # 这里是一开始截图用红色圈起来的部分,无需修改
|
||
|
)
|
||
|
res = requests.get(url)
|
||
|
json_data = json.loads(res.text)
|
||
|
|
||
|
if json_data["status"] == 0:
|
||
|
lat = json_data["results"][0]["location"]["lat"] # 纬度
|
||
|
lng = json_data["results"][0]["location"]["lng"] # 经度
|
||
|
else:
|
||
|
print(json_data["message"])
|
||
|
return "0,0", json_data["status"]
|
||
|
return lat,lng
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
res = []
|
||
|
maps = ['哈尔滨市', '齐齐哈尔市', '鸡西市', '鹤岗市', '双鸭山市', '大庆市', '伊春市', '佳木斯市', '七台河市', '牡丹江市', '黑河市', '绥化市', '大兴安岭地区']
|
||
|
|
||
|
for i in range(len(maps)):
|
||
|
Name = maps[i]
|
||
|
P1 = getPosition(Name)
|
||
|
res.append([Name, P1[0], P1[1]])
|
||
|
|
||
|
pd.DataFrame(res).to_excel(
|
||
|
"data.xlsx",
|
||
|
header=["地点", "纬度", "经度"],
|
||
|
index=None,
|
||
|
encoding="utf-8"
|
||
|
)
|