Skip to content
Browse files

added another google fight program - given n keywords, finds out who …

…won by most results
1 parent d07dfd7 commit c66043c8b59309e2740ca2086a0f6732c1075119 @pkrumins committed
Showing with 32 additions and 0 deletions.
  1. +32 −0 programs/google_fight2.py
View
32 programs/google_fight2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+#
+# Peteris Krumins (peter@catonmat.net)
+# http://www.catonmat.net -- good coders code, great reuse
+#
+# A Google Fight program. v1.0
+#
+# Released under GNU GPL
+#
+# http://www.catonmat.net/blog/python-library-for-google-search
+#
+
+import sys
+from operator import itemgetter
+from xgoogle.search import GoogleSearch, SearchError
+
+args = sys.argv[1:]
+if not args:
+ print "Usage: google_fight.py keyword one, keyword two, ..."
+ sys.exit(1)
+
+keywords = [k.strip() for k in ' '.join(args).split(',')]
+try:
+ results = [(k, GoogleSearch('"%s"' % k).num_results) for k in keywords]
+except SearchError, e:
+ print "Google search failed: %s" % e
+ sys.exit(1)
+
+results.sort(key=itemgetter(1), reverse=True)
+for res in results:
+ print "%s: %d" % res
+

0 comments on commit c66043c

Please sign in to comment.
Something went wrong with that request. Please try again.