pandas 在 Python 地理编码器中使用我的 Google Geocoding API 密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45336763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:06:28  来源:igfitidea点击:

Using my Google Geocoding API key with Python geocoder

pythongoogle-mapspandasgoogle-geocoding-api

提问by Raphael Hernandes

I'm a beginner at Python and I've been working to geocode a database using Pandas and Geocoder on Jupyter.

我是 Python 的初学者,我一直致力于在 Jupyter 上使用 Pandas 和 Geocoder 对数据库进行地理编码。

Since the df is a little long (around 3000 rows), I'd like to use Google's Geocoding API.

由于 df 有点长(大约 3000 行),我想使用 Google 的地理编码 API。

I've already created a free key, but I have no idea what I'm supposed to do with it. Help?

我已经创建了一个免费密钥,但我不知道我应该用它做什么。帮助?

By the way, my code looks like this:

顺便说一下,我的代码如下所示:

import geocoder
import pandas as pd

geo = geocoder

df=pd.read_excel('hsp2.xlsx')

df['Coordinates']=df['Address'].apply(geo.google).apply(lambda x: x.latlng if x != None else None)

df.to_csv('output.csv', sep='|', encoding='iso8859_15')

回答by Roman

You need to set the environment variable beforeyou import geocoder:

您需要导入之前设置环境变量geocoder

import os

os.environ["GOOGLE_API_KEY"] = "api_key_from_google_cloud_platform"

import geocoder

geo = geocoder.google(address)
latlong = geo.latlng

Note:

笔记:

As Murmel mentioned in the comments, environment variables containing keys (and in general) should not be set inside of your code.
If you are deploying this somewhere then set up enviroment variables in your configuration file. Or even better, as a secret in something like Kubernetes.

正如 Murmel 在评论中提到的那样,不应在您的代码中设置包含键(以及一般情况下)的环境变量。
如果您要在某处部署它,请在您的配置文件中设置环境变量。或者甚至更好,作为 Kubernetes 之类的秘密。

Else set the environment variable in bash with
export GOOGLE_API_KEY=api_key_from_google_cloud_platform

否则在 bash 中设置环境变量
export GOOGLE_API_KEY=api_key_from_google_cloud_platform

回答by Murmel

Basically there are 2 options:

基本上有2个选择:

  • passing the API KEY as environment variable:

    GOOGLE_API_KEY=YOUR-API-KEY-HERE  python your_program.py
    
  • passing the API KEY as argument:

    geocoder.google('some address', key='YOUR-API-KEY-HERE')
    
  • 将 API KEY 作为环境变量传递:

    GOOGLE_API_KEY=YOUR-API-KEY-HERE  python your_program.py
    
  • 将 API KEY 作为参数传递:

    geocoder.google('some address', key='YOUR-API-KEY-HERE')
    


Details

细节

  1. You are using the python library called geocoder, which itself is a wrapper around multiple geocoding services.

  2. If you look at the pypi page of geocoder, you can (ignoring the rendering problems) find the docsfor geocoder. In your case you probably want to have a look at the Google related part of the docs.

  3. For basic usage this seams to work even without an API KEY, but you can specify one using 2 variants:

    • Environment variable: Like Roman already showed. This approach is meant to be used to not have the API KEYin code - for security reasons. (Probably you want to upload your code into a public repository, but without exposing your API KEYto everyone.)

    • "Key" parameter:You can also provide your API KEYby specifying it using the keyparameter, like:

      geocoder.google('some address', key='YOUR-API-KEY-HERE')
      
  1. 您正在使用名为geocoder的 python 库,它本身是多个地理编码服务的包装器。

  2. 如果您查看geocoder 的 pypi 页面,您可以(忽略渲染问题)找到geocoder的文档。在您的情况下,您可能想查看文档Google 相关部分

  3. 对于基本用法,即使没有 API KEY 也可以工作,但您可以使用 2 个变体指定一个:

    • 环境变量:像Roman 已经显示。出于安全原因,此方法旨在用于在代码中不包含API KEY。(可能您想将代码上传到公共存储库,但又不想将API KEY暴露给所有人。)

    • “Key”参数:您还可以通过使用参数指定API KEY来提供它key,例如:

      geocoder.google('some address', key='YOUR-API-KEY-HERE')
      

回答by kepy97

I am agree with Romananswer. You can use that and it is working. I am bit afraid if I use geocoder in loop then google will definately block my ip address ,so I go through git hub code and found that geocoder get google api key from os.environ.get('GOOGLE_API_KEY'). You can see that in the picture:

我同意罗马的回答。你可以使用它,它正在工作。我有点害怕如果我在循环中使用地理编码器,那么谷歌肯定会阻止我的 IP 地址,所以我通过 git hub 代码发现地理编码器从os.environ.get('GOOGLE_API_KEY'). 你可以在图片中看到:

here

这里