Python 使用代理实例化geopy.geocoders.GoogleV3会导致异常
我试着这样称呼GoogleV3地理定位器:Python 使用代理实例化geopy.geocoders.GoogleV3会导致异常,python,geopy,Python,Geopy,我试着这样称呼GoogleV3地理定位器: geolocator = GoogleV3(proxies={"http": "http://user:password@ip:port"}) address, (latitude, longitude) = geolocator.geocode('address to geocode') 它提出: AttributeError: OpenerDirector instance has no __call__ method 我做错了什么?如何修复它
geolocator = GoogleV3(proxies={"http": "http://user:password@ip:port"})
address, (latitude, longitude) = geolocator.geocode('address to geocode')
它提出:
AttributeError: OpenerDirector instance has no __call__ method
我做错了什么?如何修复它?在Google V3的当前实现中,不可能将用户和密码变量直接传递给urllib2.opener(Google V3在后台使用urllib2) 下面是如何调用urllib2.opener的示例:
proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')
遗憾的是,当前的GoogleV3实现没有使用urllib2.ProxyBasicAuthHandler
因此,您需要通过修改源来扩展它:
在顶部添加:
从URLPRASE导入URLPRASE
然后查找“if self.proxies is None:”代码并将其替换为:
if self.proxies is None:
self.urlopen = urllib_urlopen
else:
params = urlparse(proxies[1])
host = params.get('hostname')
username = params.get('username')
password = params.get('password')
if host and username and password:
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password(None, host, username, password)
self.urlopen = build_opener(
ProxyHandler(self.proxies, proxy_auth_handler),
)