Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# 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:
import clipboard
def google(terms):
import requests
import cgi
url = '' + cgi.escape(terms)
r = requests.get(url, headers={'Referer': ''})
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 + ')')

This comment has been minimized.

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': ''}).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
You can’t perform that action at this time.