summaryrefslogtreecommitdiff
path: root/Chapter11ProjectLucky.py
diff options
context:
space:
mode:
Diffstat (limited to 'Chapter11ProjectLucky.py')
-rw-r--r--Chapter11ProjectLucky.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Chapter11ProjectLucky.py b/Chapter11ProjectLucky.py
new file mode 100644
index 0000000..c77858f
--- /dev/null
+++ b/Chapter11ProjectLucky.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+#Chapter 11 Project
+# lucky.py - Opens several Google search results
+
+import requests
+import sys
+import webbrowser
+import bs4
+
+print('Googling...') #Display message while retrieving the Google page
+
+res = requests.get('http://google.com/search?q=' + ' '.join(sys.argv[1:]))
+res.raise_for_status()
+
+soup = bs4.BeautifulSoup(res.text)
+linkElems = soup.select('.r a')
+
+numOpen = min(5, len(linkElems))
+for i in range(numOpen):
+ webbrowser.open('htpp://google.com' + linkElems[i].get('href'))