Skip to content

Instantly share code, notes, and snippets.

@Property404
Created June 17, 2016 18:33
Show Gist options
  • Save Property404/8c01077e683c3afd3d9ce46328733b89 to your computer and use it in GitHub Desktop.
Save Property404/8c01077e683c3afd3d9ce46328733b89 to your computer and use it in GitHub Desktop.
Open random website based on command line arguments
#! /usr/bin/env python
from random import choice
import os
import webbrowser
if os.sys.version_info[0] < 3:
from urllib import urlopen
else:
from urllib.request import urlopen
def search(terms, exclude=('bing', 'microsoft', 'msn', 'twitter', 'live.com')):
qurl = "http://www.bing.com/search?q="+"+".join(terms)+"&go=&qs=n&form=QBRE&pq=firefox&sc=0-0&sp=-1&sk="
qpage = urlopen(qurl).read().decode('UTF-8')
a = 0
searchlist = []
while True:
linkhead = qpage[a::].find("href=\"http")+a
if linkhead == a-1:
break
linktail = qpage[linkhead+20::].find("\"")+linkhead+20
a = linktail
link = qpage[linkhead+6:linktail]
ignore = False
for x in exclude:
if x in link:
ignore = True
if not ignore:
searchlist.append(link)
print(link)
return searchlist
def random_site(total_terms, n=5):
terms = []
print("Total terms="+str(total_terms))
while len(total_terms) <= n:
total_terms+=total_terms
for i in range(n):
terms.append(choice(total_terms))
total_terms.remove(terms[-1])
terms[-1]=terms[-1].replace(" ","+")
print("terms:"+str(terms))
return choice(search(terms))
#random_site(os.sys.argv[1::])
webbrowser.open(random_site(os.sys.argv[1::]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment