我在通过Google API运行了几千行后收到此错误。我认为这与请求的关闭有关。代码示例如下。运行 Windows 7 的 Python 版本 3.4.2
import urllib.request, json, csv, time
def GoogPlacDetails(placeid,key):
#making the url
AUTH_KEY = key
PLACEID = placeid
MyUrl = ('https://maps.googleapis.com/maps/api/place/details/json'
'?placeid=%s'
'&sensor=false&key=%s') % (PLACEID, AUTH_KEY)
#grabbing the JSON result
response = urllib.request.urlopen(MyUrl, timeout=10)
jsonRaw = response.read().decode('utf-8')
response.close()
jsonData = json.loads(jsonRaw)
return jsonData
如果运行
def GoogPlacDetails(placeid,key):
print "Fetch data from URL"
AUTH_KEY = key
PLACEID = placeid
fileName = 'data/googleJson.json'
url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=',PLACEID,'&sensor=false&key=',AUTH_KEY
try:
urllib2.urlopen(url)
filePut = open(fileName, 'w')
data = urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
print(e.code)
data = 0
except urllib2.URLError, e:
print(e.args)
data = 0
if data != 0: #validate url
try:
with open(fileName) as jsonGetData:
#add data
filePut.write(data)
filePut.close()
j = json.load(jsonGetData)
print "Json processed."
return 1
except Exception, e:
print e
raise
else:
pass
finally:
pass
else:
print url," Not available."
return 0
#end GoogPlacDetails
否则,请查看此处的文档以调整 Python 3 的上述代码。