Skip to content

Instantly share code, notes, and snippets.

@mrjones-plip
Last active July 29, 2025 21:13
Show Gist options
  • Save mrjones-plip/db4321e6efc899e24bd5bb39c5fa95d6 to your computer and use it in GitHub Desktop.
Save mrjones-plip/db4321e6efc899e24bd5bb39c5fa95d6 to your computer and use it in GitHub Desktop.
Google search to CSV Python script

Search google with python, save to excel

Does what it says on the tin!

Prerequistes

  1. python installed
  2. install needed libraries with something like pip install -r requirements.txt

Running

Edit search.py to have the query you want in search_query and the number of results in num_results. Be sure to escape quotes as needed. so if your search term is "this phrase" in top 10 google results, it would be defined as:

search_query = "\"this phrase\" in top 10 google results"

Run it! python search.py

File will be saved as someting like 'this phrase' in top 10 google results (2025-07-29 12:26:50).xlsx

Copyright (c) 2025 github.com/mrjones-plip
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
googlesearch-python
pandas
openpyxl
### EDIT THIS TO MEET YOUR NEEDS
search_query = "\"open collection\" library books"
num_results = 5
#### DON'T EDIT BELOW THIS ;)
from googlesearch import search
import pandas as pd
import datetime
import openpyxl
data_dict = {
'index': [],
'title': [],
'description': [],
'url': []
}
count = 1
query_success = True
try:
results_tmp = search(search_query, num_results, unique=True, advanced=True)
for item in results_tmp:
if "search?num=100" in item.url or item.title == "":
continue
else:
data_dict["index"].append(count)
data_dict["title"].append(item.title)
data_dict["description"].append(item.description)
data_dict["url"].append(item.url)
count += 1
except Exception as error:
print(f"ERROR: Search failed: '{search_query}'.\n\nError is: {repr(error)}")
query_success = False
if query_success:
time_stamp = datetime.datetime.now().strftime("%Y-%m-%d %I.%M.%S")
filename = search_query.replace("\"","'") + f" ({time_stamp}).xlsx"
df = pd.DataFrame(data_dict)
df.to_excel(filename, index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment