首页 > 解决方案 > 使用 Python 进行地理编码 - 仅从公司名称中获取地址

问题描述

我目前正在从事一个项目,其中我有一个公司名称列表,我必须获得每个公司的地址。我已经尝试过对 google API 甚至以下代码进行地理编码,但它不起作用。

> from geopy.geocoders import Nominatim
> geolocator = Nominatim(user_agent="specify_your_app_name_here")
> location = geolocator.geocode("Company name")
> print(location.address) 
> print((location.latitude, location.longitude))
> print(location.raw)

有谁知道这是怎么做到的吗?

非常感谢,

标签: pythonweb-scrapinggeocoding

解决方案


我在英国伊斯灵顿的安吉尔随机选择了一家名为 Chilango 的快餐店。然后我通过 MapQuest尝试了 Open Search (Nominatim) API 。免费且需要 API 密钥。

我模仿了一个示例 GET 请求:

import requests

res = requests.get('http://open.mapquestapi.com/nominatim/v1/search.php?key=myKey&format=json&q=Chilango+[fast food]&addressdetails=1&limit=3&viewbox=-1.99%2C52.02%2C0.78%2C50.94&exclude_place_ids=41697').json()

从文档中:

将查询字符串括在 [] 中会导致 Nominatim 进行设施搜索。例如,q=[pub] 返回 OpenStreetMap 中 type=pub 的结果。

期望的结果是我限制搜索结果的 3 个结果中的第 2 个。


推荐阅读