Skip to content

Instantly share code, notes, and snippets.

@omz
Created October 17, 2012 22:45
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save omz/3908817 to your computer and use it in GitHub Desktop.
Save omz/3908817 to your computer and use it in GitHub Desktop.
GoogleSearch
# Google Search for Pythonista (iOS)
# Searches Google and copies the first result to the clipboard as
# a Markdown link in the form [title](url).
#
# Inspired by Brett Terpstra's SearchLink:
# http://brettterpstra.com/searchlink-automated-markdown-linking-improved/
import clipboard
def google(terms):
import requests
import cgi
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&filter=1&rsz=small&q=' + cgi.escape(terms)
r = requests.get(url, headers={'Referer': 'http://bretterpstra.com'})
if r.json:
response_data = r.json.get('responseData', None)
if response_data:
result = response_data['results'][0]
output_url = result['unescapedUrl']
output_title = result['titleNoFormatting']
return output_title, output_url
if __name__ == '__main__':
terms = raw_input('Enter search terms:')
title, url = google(terms)
print 'First Google Result:'
print 'Title:', title
print 'URL:', url
clipboard.set('[' + title + ']' + '(' + url + ')')
@cclauss
Copy link

cclauss commented Jul 3, 2014

Change lines 14-16 to read: (note the call to the .json() method at the end of the first line)

r = requests.get(url, headers={'Referer': 'http://bretterpstra.com'}).json()
    if r:
        response_data = r.get('responseData', None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment