# -*- coding: utf-8 -*-
import urllib.request
import time
import json

def get_location(key, city):
     try: 
          url = "http://api.map.baidu.com/place/v2/search?q=" \
          + urllib.parse.quote(key) + "&region=" + \
          urllib.parse.quote(city) + "&output=json&ak=lQU8bheXjCBZZfb4k9fuzj6k6l1GA9l2"
          response = urllib.request.urlopen(url)
          json_str = response.read().decode('utf8')
          print(json.loads(json_str)['results'][0]['location'])
          return json.loads(json_str)['results'][0]['location']
     except:
          #打开文件
          fo = open("log.txt", "r+")
          # 在文件末尾写入一行
          fo.seek(0, 2)
          line = fo.write("百度API访问异常。" + time.time() + '\n')
          return {'lat': 0, 'lng': 0}
get_location('饭店', '北京')
