首页 > 解决方案 > 从 Geopy 库中获取纬度和经度时出错

问题描述

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="main")
location = geolocator.geocode({"postalcode": 333031})
print(location.raw)

回溯(最后一次调用):文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第 1319 行,在 do_open h.request (req.get_method()、req.selector、req.data、标头、文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py” ,第 1245 行,在请求 self._send_request(method, url, body, headers, encode_chunked) 文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client .py”,第 1291 行,在 _send_request self.endheaders(body, encode_chunked=encode_chunked) 文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client. py",第 1240 行,在 endheaders 中。_send_output(message_body, encode_chunked=encode_chunked) 文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py”,第 1008 行,在 _send_output self.send (味精)文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py”,第948行,在发送self.connect()文件“/ Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py",第 1414 行,在 connect self.sock = self._context.wrap_socket(self.sock,文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py”,第 500 行,在 wrap_socket return self.sslsocket_class._create(File"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py”,第 1040 行,在 _create self.do_handshake() 文件“/Library/Developer/CommandLineTools/Library /Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py”,第 1309 行,在 do_handshake self._sslobj.do_handshake() socket.timeout: _ssl.c:1091: 握手操作超时

在处理上述异常的过程中,又出现了一个异常:

Traceback(最近一次调用最后一次):文件“/Users/temp/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/geopy/adapters.py”,第 297 行,在 get_text page = self.urlopen(req ,超时=超时)文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第525行,打开响应= self._open(请求,数据)文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第 542 行,在 _open 结果 = self._call_chain(self .handle_open,协议,协议 + 文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第 502 行,在 _call_chain 结果 = func( *args) 文件 "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第 1362 行,在 https_open 返回 self.do_open(http.client.HTTPSConnection, req, do_open 中的文件“/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py”,第 1322 行引发 URLError(err) urllib.error.URLError:

在处理上述异常的过程中,又出现了一个异常:

回溯(最后一次调用):文件“/Users/temp/PycharmProjects/pythonProject/main.py”,第 8 行,位置 = geolocator.geocode({"postalcode": 152116}) 文件“/Users/temp/PycharmProjects /pythonProject/venv/lib/python3.8/site-packages/geopy/geocoders/nominatim.py”,第 297 行,在地理编码中返回 self._call_geocoder(url, callback, ti​​meout=timeout) 文件“/Users/temp/PycharmProjects /pythonProject/venv/lib/python3.8/site-packages/geopy/geocoders/base.py”,第 368 行,在 _call_geocoder 结果 = self.adapter.get_json(url, timeout=timeout, headers=req_headers) 文件“/ Users/temp/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/geopy/adapters.py",第 286 行,在 get_json 文本 = self.get_text(url, timeout=timeout, headers=headers) 文件中"/Users/temp/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/geopy/adapters.py”,第 315 行,在 get_text 中引发 GeocoderTimedOut(“服务超时”) geopy.exc.GeocoderTimedOut:服务定时出去

标签: pythongeopy

解决方案


推荐阅读