Created
May 20, 2018 08:52
-
-
Save ResolveWang/7ad613e262f39ddd85a241dc78da782f to your computer and use it in GitHub Desktop.
百度API:地理位置转为经纬度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
from weibospider.db.dao import WbDataOper | |
# 借助百度地图API实现地理位置的经纬度获取 | |
class LocationHelper: | |
# ak 是在 http://lbsyun.baidu.com/apiconsole/key 申请的 | |
ak = 'xxxxxx' | |
url = 'http://api.map.baidu.com/geocoder/v2/?address={}&output=json' \ | |
'&ak={}' | |
def get_lat_lng(self): | |
weibos = WbDataOper.get_weibo_with_location() | |
for weibo in weibos: | |
real_url = self.url.format(weibo.location, self.ak) | |
try: | |
r = requests.get(real_url).json() | |
lat = r['result']['location']['lat'] | |
lng = r['result']['location']['lng'] | |
print('本条微博位置为{}, 对应经纬度为{},{}'.format(weibo.location, lat, lng)) | |
WbDataOper.set_weibo_lat_lng(lat, lng, weibo) | |
time.sleep(0.5) | |
except Exception: | |
pass | |
if __name__ == '__main__': | |
helper = LocationHelper() | |
helper.get_lat_lng() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment