Skip to content

Instantly share code, notes, and snippets.

@fearsum-fyz
Created July 26, 2022 18:57
Show Gist options
  • Select an option

  • Save fearsum-fyz/86e77aa48e5e64e60cc170c5927f16ba to your computer and use it in GitHub Desktop.

Select an option

Save fearsum-fyz/86e77aa48e5e64e60cc170c5927f16ba to your computer and use it in GitHub Desktop.
Given the name of a U.S. based business, use SerpAPI to obtain its URL.
# Insert your SerpAPI API Key in lines 13 and 42
from serpapi import GoogleSearch
from difflib import SequenceMatcher
business_name = input("What is the name of the business?\n")
try:
params = {
"engine": "google_maps",
"type": "search",
"q": business_name,
"api_key": "INSERT_API_KEY_HERE",
"ll": "@37.0902,-95.7129,3z",
}
search = GoogleSearch(params)
results = search.get_dict()
if SequenceMatcher(None, results['local_results'][0]['title'], business_name).ratio() > 0.55:
final_result = results['local_results'][0]['website']
elif SequenceMatcher(None, results['local_results'][1]['title'], business_name).ratio() > 0.55:
final_result = results['local_results'][1]['website']
elif SequenceMatcher(None, results['local_results'][2]['title'], business_name).ratio() > 0.55:
final_result = results['local_results'][2]['website']
elif SequenceMatcher(None, results['local_results'][3]['title'], business_name).ratio() > 0.55:
final_result = results['local_results'][3]['website']
elif SequenceMatcher(None, results['local_results'][4]['title'], business_name).ratio() > 0.55:
final_result = results['local_results'][4]['website']
else:
raise Exception('Not matching.')
print(f"{business_name}'s website is at {final_result}.")
except:
params = {
"q" : business_name,
"hl" : "en",
"gl" : "us",
"google_domain" : "google.com",
"api_key" : "INSERT_API_KEY_HERE",
}
search = GoogleSearch(params)
results = search.get_dict()
final_result = results['organic_results'][0]['link']
print(f"{business_name}'s website is at {final_result}.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment