-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle.py
More file actions
executable file
·30 lines (25 loc) · 794 Bytes
/
google.py
File metadata and controls
executable file
·30 lines (25 loc) · 794 Bytes
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
"""
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 webbrowser, sys, pyperclip, requests, bs4
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()