Advertisement
gsonego

Python: GET Request

Oct 22nd, 2019
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # importing the requests library
  2. import requests
  3.  
  4. # api-endpoint
  5. URL = "http://maps.googleapis.com/maps/api/geocode/json"
  6.  
  7. # location given here
  8. location = "delhi technological university"
  9.  
  10. # defining a params dict for the parameters to be sent to the API
  11. PARAMS = {'address':location}
  12.  
  13. # sending get request and saving the response as response object
  14. r = requests.get(url = URL, params = PARAMS)
  15.  
  16. # extracting data in json format
  17. data = r.json()
  18.  
  19.  
  20. # extracting latitude, longitude and formatted address  
  21. # of the first matching location
  22. latitude = data['results'][0]['geometry']['location']['lat']
  23. longitude = data['results'][0]['geometry']['location']['lng']
  24. formatted_address = data['results'][0]['formatted_address']
  25.  
  26. # printing the output
  27. print("Latitude:%s\nLongitude:%s\nFormatted Address:%s"
  28.       %(latitude, longitude,formatted_address))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement