Skip to content

Instantly share code, notes, and snippets.

@hiway
Last active December 12, 2015 03:28
Show Gist options
  • Save hiway/4706506 to your computer and use it in GitHub Desktop.
Save hiway/4706506 to your computer and use it in GitHub Desktop.
Uses a [deprecated?] Google API to search for your query and returns URL of the first result. I use this with TextExpander to quickly put in URLs of services I'm mentioning in chats, emails or blog posts.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
def get_search_results(query):
url = "http://ajax.googleapis.com/ajax/services/search/web"
payload = dict(
v='1.0',
rsz='large',
q=query,
)
r = requests.get(url, params=payload)
return json.loads(r.text)
r = get_search_results(' '.join(sys.argv[1:]))
print r['responseData']['results'][0]['url']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment