Skip to content

Instantly share code, notes, and snippets.

@psy901
Created January 18, 2018 06:00
Show Gist options
  • Save psy901/963175ca22c703998f0ca2584482ab3d to your computer and use it in GitHub Desktop.
Save psy901/963175ca22c703998f0ca2584482ab3d to your computer and use it in GitHub Desktop.
Automated googling with given search term and returning result links
import selenium.webdriver as webdriver
from time import sleep
def get_results(search_term):
MAX_LINKS = 2 # Set the maximum number of links to gather
# Use google to search the search term
url = "https://www.google.com"
browser = webdriver.Chrome()
browser.get(url)
search_bx = browser.find_element_by_class_name('gsfi')
search_bx.send_keys(search_term)
search_bx.submit()
try:
# links = browser.find_elements_by_class_name('//rc/r')
links = browser.find_elements_by_xpath('//div//h3//a')
except:
print("error")
results = []
# retrieves first two links
for link in links[0:MAX_LINKS]:
# href = link.get_attribute('r')
href = link.get_attribute('href')
results.append(href)
browser.close()
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment