输入数据到网站并提取结果
Input data to website and extract result
我希望有人能指导我完成这个小项目。
我在 excel 文件中有一个地址列表,我想将地址(可以是邮政编码)单独粘贴到下面链接的网站中,然后提取 Long/Lat 坐标,然后粘贴它们到同一 excel 文件中的地址。
这可能吗?我假设我会使用 python、beautifulsoup 的组合?我在某处读到有关机械化的内容。将不胜感激。
上述网站使用 Google 地图 API 将地址转换为坐标(称为地理编码)。为了能够使用 Google 地图 API,您需要一个 API 密钥
您可以按照有关如何获取 API 密钥的说明 here 进行操作。
完成后,您需要安装 googlemaps
Python 软件包才能使用 API:
pip install googlemaps
以下代码将地址转换为坐标并打印出给定地址所在的坐标:
import googlemaps
key = 'YOUR_API_KEY'
address = '1600 Amphitheatre Parkway' # Google's address
gmaps = googlemaps.Client(key=key)
# Convert the given address to coordinates. You can use gmaps.reverse_geocode((lat, lng))
# to convert the coordinates back to an address
geocode_result = gmaps.geocode(address)
lat = geocode_result[0]['geometry']['location']['lat'] # Get the latitude
lng = geocode_result[0]['geometry']['location']['lng'] # Get the longitude
print(address, 'is at', (lat, lng))
我希望有人能指导我完成这个小项目。
我在 excel 文件中有一个地址列表,我想将地址(可以是邮政编码)单独粘贴到下面链接的网站中,然后提取 Long/Lat 坐标,然后粘贴它们到同一 excel 文件中的地址。
这可能吗?我假设我会使用 python、beautifulsoup 的组合?我在某处读到有关机械化的内容。将不胜感激。
上述网站使用 Google 地图 API 将地址转换为坐标(称为地理编码)。为了能够使用 Google 地图 API,您需要一个 API 密钥
您可以按照有关如何获取 API 密钥的说明 here 进行操作。
完成后,您需要安装 googlemaps
Python 软件包才能使用 API:
pip install googlemaps
以下代码将地址转换为坐标并打印出给定地址所在的坐标:
import googlemaps
key = 'YOUR_API_KEY'
address = '1600 Amphitheatre Parkway' # Google's address
gmaps = googlemaps.Client(key=key)
# Convert the given address to coordinates. You can use gmaps.reverse_geocode((lat, lng))
# to convert the coordinates back to an address
geocode_result = gmaps.geocode(address)
lat = geocode_result[0]['geometry']['location']['lat'] # Get the latitude
lng = geocode_result[0]['geometry']['location']['lng'] # Get the longitude
print(address, 'is at', (lat, lng))