Skip to content

Commit 36395ab

Browse files
author
Ankit Agarwal
committed
Code to perform google search from terminal
1 parent d430420 commit 36395ab

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

google.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import webbrowser, sys, pyperclip, requests, bs4
2+
3+
#
4+
# Opens 1 (min) to 5 (max) tabs in browser with top search links.
5+
# (One tab for each link)
6+
#
7+
def main():
8+
if len(sys.argv) > 1:
9+
keyword = ' '.join(sys.argv[1:])
10+
else:
11+
# if no keyword is entered, the script would search for the keyword
12+
# copied in the clipboard
13+
keyword = pyperclip.paste()
14+
15+
res=requests.get('http://google.com/search?q='+ ' '.join(keyword))
16+
res.raise_for_status()
17+
soup = bs4.BeautifulSoup(res.text)
18+
linkElems = soup.select('.r a')
19+
numOpen = min(5, len(linkElems))
20+
21+
for i in range(numOpen):
22+
webbrowser.open('http://google.com' + linkElems[i].get('href'))
23+
24+
if __name__ == '__main__':
25+
main()

0 commit comments

Comments
 (0)