Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Zapier CSV Search
from googleapiclient.discovery import build
import pprint
import csv
my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
my_cse_id = "xxxxxxxxxxxxxxx:xxxxxxxxxxxxx"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
toSearch = []
csvArray = []
c = 1
with open('zapier.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
if c != 1:
toSearch.append(row[0])
csvArray.append(row)
c += 1
for i in toSearch:
results = google_search(i, my_api_key, my_cse_id, num=1)
csvArray[toSearch.index(i)+1][1] = results[0]['link']
with open('zapier.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in csvArray:
spamwriter.writerow(row);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment