質問

Hello I am trying to use the google places api to get a list of all the McDonalds in Germany (weird exercise I know). This is my code (the box is smaller than the actual box for Germany so it does not take a long time to iterate):

import urllib2, simplejson
GOOGLE_API_KEY = 'my google api'


lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575

lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0

lat_incr = -.29
lng_incr = .29

while lng_curr<lng_SE:
    lng_curr = lng_NW
    while lat_curr > lng_SE:
        curr_location = str(lat_curr) + "," + str(lng_curr)
        url='https://maps.googleapis.com/maps/api/place/search/json?location=' + curr_location + '&sensor=false&key=' + GOOGLE_API_KEY + '&radius=20000&types=restaurant&name=mcdonalds'

        response = urllib2.urlopen(url)
        result = response.read()
        d = simplejson.loads(result)
        lng_curr += lng_incr
    lat_curr += lat_incr

print (d)

And this is the output I got:

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 404, in open
    response = self._open(req, data)
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 422, in _open
    '_open', req)
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1222, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1184, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol>

Any ideas on what am I doing wrong? Thanks!

役に立ちましたか?

解決

My guess would be you've reached the usage limit.

If you try this, you'll see that your loop does never abort:

lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575

lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0

lat_incr = -.29
lng_incr = .29

while lng_curr<lng_SE:
    lng_curr = lng_NW
    while lat_curr > lng_SE:
        curr_location = str(lat_curr) + "," + str(lng_curr)
        print curr_location
        lng_curr += lng_incr
    lat_curr += lat_incr

What you probably want is something like this:

...
while lng_curr < lng_SE:
    lat_curr = lat_NW
    while lat_curr > lat_SE:
        curr_location = str(lat_curr) + "," + str(lng_curr)
        ...
        lat_curr += lat_incr
    lng_curr += lng_incr
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top