用百度地图API爬取一个地理位置的经纬度如:朝鲜
其代码如下:
import requests
import json
import re
AK='自己的ak'
address='朝鲜'
url='http://api.map.baidu.com/geocoding/v3/?address={}&output=json&ak={}&callback=showLocation'.format(address,AK)
res = requests.get(url)
print(res.text)
运行结果为:
showLocation&&showLocation({"status":0,"result":{"location":{"lng":108.42128307895369,"lat":23.934177647695177},"precise":0,"confidence":50,"comprehension":0,"level":"村庄"}}
因为想利用folium包在地图上对该位置进行标注发现:
该位置对应的并非朝鲜,而是地图上广西的一个地方:
于是将刚刚从api上获取的经纬度进行逆地理位置查询:
lat='23.934177647695177'
lng='108.42128307895369'
url=url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak={}&output=json&coordtype=wgs84ll&location={},{}'.format(AK,lat,lng)
res=requests.get(url)
print(res.text)
address=json.loads(res.text)['result']['formatted_address']
print('\n')
print('address is ',address)
运行结果为:
{"status":0,"result":{"location":{"lng":108.43201338669323,"lat":23.93732458871083},"formatted_address":"广西壮族自治区南宁市马山县","business":"","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"广西壮族自治区","city":"南宁市","city_level":2,"district":"马山县","town":"","town_code":"","distance":"","direction":"","adcode":"450124","street":"","street_number":""},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":261}}
address is 广西壮族自治区南宁市马山县
果然是广西的地方,与前面的就矛盾了呀,请问为什么会这样