Skip to content

Instantly share code, notes, and snippets.

@NathanLawrence
Created August 24, 2017 14:06
Show Gist options
  • Save NathanLawrence/b6859eb718ab295c339df2ff7abe6708 to your computer and use it in GitHub Desktop.
Save NathanLawrence/b6859eb718ab295c339df2ff7abe6708 to your computer and use it in GitHub Desktop.
Put First Google Result in a New CSV Next to Old CSV Full of Search Terms
import os
import csv
from tqdm import tqdm
from google import search
file_name = "blah.csv"
output_file_name = "out.csv"
input_file = csv.DictReader(open(file_name, 'rU'), dialect='excel')
out_file = csv.DictWriter(open(output_file_name, 'w'), fieldnames=['query', 'result_url'])
num_rows = len(open(file_name, 'rU').readlines())
out_file.writeheader()
for row in tqdm(input_file, total=num_rows):
search_resp = ''
for url in search(row['query'], stop=1):
search_resp = url
out_file.writerow({'query': row['query'], 'result_url': search_resp})
beautifulsoup4==4.6.0
google==1.9.3
tqdm==4.15.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment