Clayton Tosatti
Clayton Tosatti

Reputation: 196

Error Using geopy library

I have the following question, I want to set up a routine to perform iterations inside a dataframe (pandas) to extract longitude and latitude data, after supplying the address using the 'geopy' library.

The routine I created was:

import time
from geopy.geocoders import GoogleV3
import os

arquivo = pd.ExcelFile('path')
df = arquivo.parse("Table1")

def set_proxy():
    proxy_addr = 'http://{user}:{passwd}@{address}:{port}'.format(
        user='usuario', passwd='senha', 
        address='IP', port=int('PORTA'))
    os.environ['http_proxy'] = proxy_addr
    os.environ['https_proxy'] = proxy_addr

def unset_proxy():
    os.environ.pop('http_proxy')
    os.environ.pop('https_proxy')

set_proxy()

geo_keys = ['AIzaSyBXkATWIrQyNX6T-VRa2gRmC9dJRoqzss0'] # API Google
geolocator = GoogleV3(api_key=geo_keys )

for index, row in df.iterrows():
    location = geolocator.geocode(row['NO_LOGRADOURO'])
    time.sleep(2)
    lat=location.latitude
    lon=location.longitude
    timeout=10)
    address = location.address
    unset_proxy()
    print(str(lat) + ', ' + str(lon))

The problem I'm having is that when I run the code the following error is thrown:

GeocoderQueryError: Your request was denied.

I tried the creation without passing the key to the google API, however, I get the following message.

KeyError: 'http_proxy'

and if I remove the unset_proxy () statement from within the for, the message I receive is:

GeocoderQuotaExceeded: The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.

But I only made 5 requests today, and I'm putting a 2-second sleep between requests. Should the period be longer?

Any idea?

Upvotes: 0

Views: 839

Answers (1)

KostyaEsmukov
KostyaEsmukov

Reputation: 898

  1. api_key argument of the GoogleV3 class must be a string, not a list of strings (that's the cause of your first issue).
  2. geopy doesn't guarantee the http_proxy/https_proxy env vars to be respected (especially the runtime modifications of the os.environ). The advised (by docs) usage of proxies is:

    geolocator = GoogleV3(proxies={'http': proxy_addr, 'https': proxy_addr})
    

PS: Please don't ever post your API keys to the public. I suggest to revoke the key you've posted in the question and generate a new one, to prevent the possibility of it being abused by someone else.

Upvotes: 1

Related Questions