【问题标题】:Instantiating geopy.geocoders.GoogleV3 with proxies causes exception使用代理实例化 geopy.geocoders.GoogleV3 会导致异常
【发布时间】:2013-10-30 11:36:28
【问题描述】:

我正在尝试这样调用 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

我做错了什么?如何解决?

【问题讨论】:

    标签: python geopy


    【解决方案1】:

    在当前的 GoogleV3 实现中,无法将用户和密码变量直接传递给 urllib2.opener(GoogleV3 在后台使用 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 。

    所以,你需要通过修改源来扩展它:https://github.com/geopy/geopy/blob/master/geopy/geocoders/base.py 在顶部添加:

    从 urlparse 导入 urlparse

    然后找到'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),
                )
    

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2014-08-03
      • 1970-01-01
      • 2013-12-05
      • 2014-03-08
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多