【问题标题】:google places api in python is not working谷歌在 python 中放置 api 不起作用
【发布时间】:2014-03-11 20:58:47
【问题描述】:

您好,我正在尝试使用 google places api 来获取德国所有麦当劳的列表(我知道的奇怪练习)。这是我的代码(盒子比德国的实际盒子小,所以迭代不需要很长时间):

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)

这是我得到的输出:

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>

关于我做错了什么有什么想法吗?谢谢!

【问题讨论】:

    标签: python json web-scraping google-places-api


    【解决方案1】:

    我猜你已经到达usage limit

    如果你试试这个,你会发现你的循环永远不会中止:

    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
    

    你可能想要的是这样的:

    ...
    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
    

    【讨论】:

    • 限制是 100,000 个请求,我还没有达到。你的建议确实有效。所以我的 lat 和 lng 增量的顺序是错误的?
    • 是的,你的增量是错误的。我只是猜测你可能已经达到了使用限制,因为如果你启动原始程序,它会进入无限循环并且可以快速生成大量请求......除此之外,它应该可以工作。
    • 谢谢,这很有帮助。
    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多