Python Forum

Full Version: Not sure how to debug this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to attach a screenshot to my post and can't a way to do this. Is this possible?

Thanks
We don't need screenshots. Copy/paste your code in code tags and the full traceback in error tags. See BBcode for more info.
This code below throws exceptions.

import urllib.parse
import requests
import json

main_api = 'http://maps.googleis.com/maps/api/geocode/json?'

address = 'lhr'
url = main_api + urllib.parse.urlencode({'address': address})

json_data = requests.get(url).json()

print(json_data)




HERE ARE THE EXCEPTOINS
=====================================================================================
C:\Python34\python.exe "C:/Development/Python Apps/TestApp/MyPythonApp/JSONApp.py"
Traceback (most recent call last):
  File "C:/Development/Python Apps/TestApp/MyPythonApp/JSONApp.py", line 9, in <module>
    json_data = requests.get(url).json()
  File "C:\Python34\lib\site-packages\requests\models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Python34\lib\site-packages\simplejson\__init__.py", line 517, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "C:\Python34\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
  File "C:\Python34\lib\site-packages\simplejson\scanner.py", line 79, in scan_once
    return _scan_once(string, idx)
  File "C:\Python34\lib\site-packages\simplejson\scanner.py", line 70, in _scan_once
    raise JSONDecodeError(errmsg, string, idx)
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1
You know that the url address is wrong googleis?
API doc.
For this use Requests.

Here a demo,i use string formatting to make changes in url address.
import requests

address = '1600 Amphitheatre Parkway Mountain View'
api_key = 'Your API key'
url = 'https://maps.googleapis.com/maps/api/geocode/json?address={}{}'.format(''.join(address.split()), api_key)
url_get = requests.get(url)
loc = url_get.json()
What you get back is a Python dictionary also contain list when come from JSON.
Test run.
>>> loc['results'][0]['geometry']
{'bounds': {'northeast': {'lat': 37.42198310000001, 'lng': -122.0853195},
            'southwest': {'lat': 37.4214139, 'lng': -122.0860042}},
 'location': {'lat': 37.4216548, 'lng': -122.0856374},
 'location_type': 'ROOFTOP',
 'viewport': {'northeast': {'lat': 37.4230474802915, 'lng': -122.0843128697085},
              'southwest': {'lat': 37.4203495197085,
                            'lng': -122.0870108302915}}}

>>> loc['results'][0]['geometry']['location']
{'lat': 37.4216548, 'lng': -122.0856374}

>>> loc['results'][0]['geometry']['location']['lat']
37.4216548