-2

i want to scrap google map results using serpapi here is the suggested code: from serpapi import GoogleSearch

from serpapi import GoogleSearch

params = {
  "api_key": "API_KEY",
  "engine": "google_maps",
  "q": "company",
  "google_domain": "google.com",
  "hl": "en",
  "ll": "@37.5393407,36.707705,11z",
  "type": "search"
}

client = GoogleSearch(params)

data = client.get_dict()

print('data',data)

this successfully extract the results

but i want to save them as a csv file to could be able to analyse them

how can i access details presented in data such as adresses or webpages and so on

1 Answers1

0

To save extracted data to CSV you can use pandas and to_csv() method. Note that pandas needs a list to be passed to DataFrame.

If you'll be passing dict, you might encounter ValueError: If using all scalar values, you must pass an index error. To solve it, you need to add index=[0] to your DataFrame.

from serpapi import GoogleSearch
import pandas as pd

params = {
  "api_key": "...",
  "engine": "google_maps",
  "q": "company",
  "google_domain": "google.com",
  "hl": "en",
  "ll": "@37.5393407,36.707705,11z",
  "type": "search"
}

search = GoogleSearch(params)
results = search.get_dict()

# access "local_results" or other data you need
df = pd.DataFrame(results["local_results"])

# index=false to drop default pandas row numbers
df.to_csv(f"google-maps-{params['q']}-data.csv", index=False)

CSV google-maps-company-data.csv output for "local_results":

enter image description here

Let me know if you have any issues.

Dmitriy Zub
  • 1,398
  • 8
  • 35