close

python 換成 3.3 有點不習慣

import urllib.parse
import urllib.request
import json

serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    try:
        address = input('Enter location: ')
    except:
        print("Error !!")
        continue
    if len(address) < 1 : break

    url = serviceurl + urllib.parse.urlencode({'sensor':'false', 'address': address})
    print ('Retrieving', url)
    try:
        uh = urllib.request.urlopen(url)
        data = uh.read().decode('utf8')
    except:
        print("Error !!")
        continue
    print ('Retrieved',len(data),'characters')
    js = json.loads(str(data))
    if js["status"] == "OK":
        print ("===========================")
        lat = js["results"][0]["geometry"]["location"]["lat"]
        lng = js["results"][0]["geometry"]["location"]["lng"]
        print ('lat',lat,'lng',lng)
        location = js['results'][0]['formatted_address']
        print (location)
    else:
        print("Error !!")

----------------------------------------------------------------------------------------------------------------------------------------------------------

[python-echochio.rhcloud.com repo]\> python test.py
Enter location: 台中
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=%E5%8F%B0%E4%B8%AD
Retrieved 1404 characters
===========================
lat 24.1477358 lng 120.6736482
Taichung City, Taiwan
Enter location: 公益路
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=%E5%85%AC%E7%9B%8A%E8%B7%AF
Retrieved 1548 characters
===========================
lat 24.1508656 lng 120.6616736
Gongyi Rd, Taichung City, Taiwan
 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 echochio 的頭像
    echochio

    echochio

    echochio 發表在 痞客邦 留言(0) 人氣()