如何用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:
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.
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!
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.
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_cacheor 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

