Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

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