Skip to content

Instantly share code, notes, and snippets.

@z-------------
Last active August 29, 2015 14:10
Show Gist options
  • Save z-------------/bb757db3616dfb6e6fde to your computer and use it in GitHub Desktop.
Save z-------------/bb757db3616dfb6e6fde to your computer and use it in GitHub Desktop.
Command line interface for Google, written in Python
import os
import sys
import webbrowser
from urllib.request import Request, urlopen
import urllib.parse
from bs4 import BeautifulSoup
os.system("clear")
try:
query = sys.argv[1]
i = 2
while i < len(sys.argv):
query += " " + sys.argv[i]
i += 1
q = Request("https://www.google.com/search?q=" + urllib.parse.quote(query))
q.add_header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.00 (KHTML, like Gecko) Chrome/39")
html = urlopen(q).read()
soup = BeautifulSoup(html)
link_elems = soup.select("li.g h3 a")
links = []
print("\n")
for link_elem in link_elems:
text = link_elem.text
url = link_elem["href"]
links.append({"text": text, "url": url})
print("[%(index)s] %(text)s | %(url)s\n" % {"index": len(links),"text": text, "url": url})
webbrowser.open(links[int(input("Open link: "))-1]["url"])
except:
print(";__; i confuse")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment