forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.py
executable file
·37 lines (29 loc) · 881 Bytes
/
google.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Author: Ankit Agarwal (ankit167)
Usage: python google.py <keyword>
Description: Script googles the keyword and opens
top 5 (max) search results in separate
tabs in the browser
Version: 1.0
"""
import sys
import webbrowser
import bs4
import pyperclip
import requests
def main():
if len(sys.argv) > 1:
keyword = " ".join(sys.argv[1:])
else:
# if no keyword is entered, the script would search for the keyword
# copied in the clipboard
keyword = pyperclip.paste()
res = requests.get("http://google.com/search?q=" + keyword)
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("http://google.com" + linkElems[i].get("href"))
if __name__ == "__main__":
main()