As an alternative solution, you can use API, such as Google Maps Place Results API from SerpApi (paid API with a free plan that handles blocks and parsing on their backend).
To begin with, we create a list of the necessary addresses and a list with coordinates to them (the coordinates can be found in the URL of the address we need):
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
# those coordinates are taken from the Google Maps URL
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
Next, we loop through the lists using zip()
, get the full address from API response and use regular expression to extract the name of the city:
city_name = re.search(r'\d+\s(\w+),', results['place_results']['address']).group(1)
Check full code in online IDE.
from serpapi import GoogleSearch
import json, re, os
# addresses from which we want to extract the name of the cities
addresses = [
'Bälliz 22, Switzerland',
'Blümlisalpstrasse 36, Switzerland',
'Stauffacherstrasse 105, Switzerland',
'Am Wasser 3, Switzerland',
'Ringstrasse 18, Switzerland'
]
# GPS coordinates of location where you want your q (query) to be applied
geo_coordinates = [
'@46.7600484,7.6155472,14.08z',
'@46.7810488,7.574276,14z',
'@47.3792421,8.5218228,16z',
'@47.4039247,8.5970111,16z',
'@47.4139972,9.190949,13z'
]
for address, coordinates in zip(addresses, geo_coordinates):
params = {
"api_key": "...", # serpapi key, https://serpapi.com/manage-api-key
"engine": "google_maps", # SerpApi search engine
"type": "search", # list of results for the query
"google_domain": "google.com", # google domain
"q": address, # query
"hl": "en", # language
"ll": coordinates # GPS coordinates
}
search = GoogleSearch(params) # where data extraction happens on the backend
results = search.get_dict() # JSON -> Python dict
city_name = re.search(r'\d+\s(\w+),', results['place_results']['address']).group(1)
print(f'City name: {city_name}')
Example output:
City name: Thun
City name: Heimberg
City name: Zürich
City name: Dübendorf
City name: Neuenhof
There's a Using Google Maps Place Results API from SerpApi using Python blog post if you need a little bit more code explanation and how to extract other data.
Disclaimer, I work for SerpApi.