You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python获取各类场所地址?寻求geopy替代方案

Hey there! I totally get it—geopy works great for standard locations, but it can hit walls when you're hunting down niche venues, local mom-and-pop spots, or specific commercial places like malls. Let’s break down some practical alternatives that’ll help you pull addresses for almost any place you need:

1. Google Maps Geocoding API

This is my go-to for broad coverage—it’s built to handle everything from big chain restaurants to tiny local shops. The tradeoff? You’ll need a Google Cloud API key (they offer a free tier for low-volume use).

Here’s a quick example using requests:

import requests

API_KEY = "your-google-api-key"
place_name = "Joe's Corner Diner, Chicago"

url = f"https://maps.googleapis.com/maps/api/geocode/json?address={place_name}&key={API_KEY}"
response = requests.get(url).json()

if response['status'] == 'OK':
    # Extract formatted address
    address = response['results'][0]['formatted_address']
    print(f"Found address: {address}")
else:
    print(f"Couldn't find address: {response['status']}")

Pro tip: Refine searches with parameters like region or type (e.g., type=restaurant) to get more precise results.

2. OpenStreetMap Nominatim API

If you don’t want to deal with API keys or paid tiers, Nominatim is a fantastic free, open-source option. It pulls data from OpenStreetMap, which is packed with community-contributed local spots.

Example code:

import requests

place_name = "Greenwood Mall, Nashville"
url = f"https://nominatim.openstreetmap.org/search?q={place_name}&format=json&limit=1"

# Add a user-agent to comply with Nominatim's terms
headers = {'User-Agent': 'MyPlaceScraper/1.0 (your-email@example.com)'}
response = requests.get(url, headers=headers).json()

if response:
    address = response[0]['display_name']
    print(f"Address: {address}")
else:
    print("No results found")

Important: Nominatim has rate limits (1 request per second for non-commercial use), so don’t spam requests. Cache results if you’re querying multiple places!

3. Yelp Fusion API

If your focus is restaurants, bars, or shopping malls, Yelp’s API is gold—it’s tailored for commercial venues and includes detailed address data, plus reviews and operating hours.

You’ll need a Yelp API key (free to get with a developer account):

import requests

API_KEY = "your-yelp-api-key"
search_term = "vegan bakery"
location = "Portland, OR"

url = "https://api.yelp.com/v3/businesses/search"
headers = {'Authorization': f'Bearer {API_KEY}'}
params = {'term': search_term, 'location': location, 'limit': 5}

response = requests.get(url, headers=headers, params=params).json()

for business in response['businesses']:
    print(f"Name: {business['name']}")
    print(f"Address: {', '.join(business['location']['display_address'])}")
    print("---")

This is perfect when you need more than just an address—you get extra context about the venue too.

4. Web Scraping (Use Sparingly!)

For super-local spots that don’t show up in APIs (like a neighborhood coffee shop with no online presence), scraping might be your last resort. But always respect a site’s robots.txt and don’t overload their servers.

Here’s a quick example with requests and BeautifulSoup:

import requests
from bs4 import BeautifulSoup

url = "https://local-coffee-shop-example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Look for address elements (depends on the site's structure)
address_element = soup.find('div', class_='address')
if address_element:
    print(f"Address: {address_element.text.strip()}")
else:
    print("Address not found on page")

Heads up: Scraping can break if the site updates its layout, so it’s not the most reliable long-term solution.

Quick Tips for All Methods

  • Cache results: Store addresses you’ve already fetched to avoid redundant API calls (use something like functools.lru_cache or a simple JSON file).
  • Handle edge cases: Always check if the API returns results—don’t assume every query will work.
  • Be specific: The more details you include in your search (city, neighborhood, zip code), the better the results you’ll get.

内容的提问来源于stack exchange,提问作者Tanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:21:06