구글 지도 API 를 통해 구글 방향을 쿼리하지 무중단으로 싶다. 예를 들어, la, 시카고 (il 이 요청을 간의 라우트를 계산 ca 의 중간점은 두 개를 통해 조플린, mo, 오클라호마 시티, 확인:
http://maps.googleapis.com/maps/api/directions/json? 출처 = = = 조플린, MO, 웨이포인트, 대상, 시카고, IL& Los+Angeles CA& Oklahoma+City, 센서, OK& = false
그 결과 되돌려줍니다 [에 JSON format] [1].
Python 이 작업을 수행할 수 있습니까? 이러한 요청을 받은 결과, 구문 분석, i want to send it.
[1]: https://developers.google.com/maps/documentation/directions/intro = 1 # 경유지를 치프?
내가 이 멋진 요청 라이브러리를 사용하는 것이 좋습니다.
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON 응답을 내용: [http://docs.python-requests.org/en/latest/user/quickstart/ # json 응답을 컨텐트입니다] [2]
[2]: http://docs.python-requests.org/en/latest/user/quickstart/ # json 응답을 컨텐트입니다
이 ['요청'] (https://requests.kennethreitz.org//en/latest/) 이 JSON 데이터를 처리하는 파이썬 모듈 및 디코딩 하기 때문에 모두 검색하는 내장 JSON 디코더 (decoder). 예를 들어보겠습니다 빼앗아 [이 module& # 39 의 문서용으로] [1]:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
그래서 쓸 수 있는 일부 별도의 디코딩 JSON 모듈에서는 사용할 수 없다. [1]: https://requests.kennethreitz.org//en/latest/user/quickstart/ # json 응답을 컨텐트입니다
['요청'] (http://docs.python-requests.org/en/latest/index.html) 에 내장된 .j슨 () '방법'
import requests
requests.get(url).json()
더 나은 결과를 찾을 수 있도록 요청률 사용하여 라이브러리, 예쁜 인쇄인쇄 시동키는 / 원하는 값을 사용하여 데이터를 분석할 수 푸십시오 후 네스트된 for 루프. 예를 들어, 내가 푸십시오 단계별 운전경로 안내.
import json, requests, pprint
url = 'http://maps.googleapis.com/maps/api/directions/json?'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
data = requests.get(url=url, params=params)
binary = data.content
output = json.loads(binary)
# test to see if the request was valid
#print output['status']
# output all of the results
#pprint.pprint(output)
# step-by-step directions
for route in output['routes']:
for leg in route['legs']:
for step in leg['steps']:
print step['html_instructions']
이거 드세요.
import requests
import json
# Goole Maps API.
link = 'http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false'
# Request data from link as 'str'
data = requests.get(link).text
# convert 'str' to Json
data = json.loads(data)
# Now you can access Json
for i in data['routes'][0]['legs'][0]['steps']:
lattitude = i['start_location']['lat']
longitude = i['start_location']['lng']
print('{}, {}'.format(lattitude, longitude))