如何扩展Python脚本获取谷歌地图多地点查询完整结果列表?
I'm developing a Python script to query Google Maps locations. My queries return multiple results instead of a single location (example query: Restaurants in New Delhi) and I want to save the full list of results. Currently, the code I've tried only fetches a single location:
import requests import ast def google_maps(req): req.replace(' ','+') uri = 'https://www.google.com/maps/search/' url = uri+req ret = requests.get(url).text scrape = '['+ret.split('cacheResponse([[')[1].split(',[')[0].split(',',1)[1] location = ast.literal_eval(scrape) print(location) google_maps('Restaurants in New Delhi')
How can I expand this code to get the complete list of results returned by Google Maps?
First off, let's break down why your current code only pulls one result: your string-splitting logic targets a tiny slice of the page's content that only captures the first entry. Google Maps loads search results into a nested JavaScript array, so we need to adjust how we extract that data to grab the full set.
Here's a revised version of your script that should capture all visible results from the search page:
import requests import ast def google_maps(req): # Fix: actually assign the replaced string back to req (your original line didn't save the change) req = req.replace(' ', '+') uri = 'https://www.google.com/maps/search/' url = uri + req # Add headers to mimic a real browser (Google often blocks bare requests) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' } ret = requests.get(url, headers=headers).text # Adjust extraction logic to target the full results array try: # Locate the initialization state that contains search results start_marker = 'window.APP_INITIALIZATION_STATE=' start_idx = ret.find(start_marker) + len(start_marker) end_idx = ret.find(';', start_idx) # Extract and parse the JSON-like structure app_state = ret[start_idx:end_idx] data = ast.literal_eval(app_state) # Navigate nested structure to get the place results (path is specific to current Google Maps structure) results = data[3][6][0][14][0][0][0] print(f"Found {len(results)} results:") for idx, place in enumerate(results, 1): # Pull out key details (name, address - adjust indices if needed) name = place[11] address = place[17] print(f"{idx}. {name} - {address}") except IndexError as e: print(f"Error extracting results: Google might have updated their page structure. Error: {e}") except Exception as e: print(f"Unexpected error: {e}") google_maps('Restaurants in New Delhi')
Key Notes:
- Browser Headers: I added a
User-Agentheader to make your request look like it's coming from a real browser—Google frequently blocks requests without this. - Fragility Warning: This method depends entirely on Google Maps' current page structure. If Google updates how they load or store results, this script will break. For a reliable, long-term solution, use the Google Places API—it's built for this exact use case, handles pagination, and avoids scraping-related issues.
- Index Adjustments: The nested indices (
data[3][6][0][14][0][0][0]) point to where results live right now. If the script stops working, you'll need to inspect the page source to find the new path to the results array.
If you switch to the official API, you'll need an API key from Google Cloud, then use the textsearch endpoint to get paginated, structured results—this is the recommended approach for production code.
内容的提问来源于stack exchange,提问作者Satyen Pandita

