Skip to content

Commit edb07f2

Browse files
Merge pull request #145 from ankit167/Google_from_terminal
Code to perform google search from terminal
2 parents 2ef8baf + 1e95ff0 commit edb07f2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

google.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Author: Ankit Agarwal (ankit167)
3+
Usage: python google.py <keyword>
4+
Description: Script googles the keyword and opens
5+
top 5 (max) search results in separate
6+
tabs in the browser
7+
Version: 1.0
8+
"""
9+
10+
import webbrowser, sys, pyperclip, requests, bs4
11+
12+
def main():
13+
if len(sys.argv) > 1:
14+
keyword = ' '.join(sys.argv[1:])
15+
else:
16+
# if no keyword is entered, the script would search for the keyword
17+
# copied in the clipboard
18+
keyword = pyperclip.paste()
19+
20+
res=requests.get('http://google.com/search?q='+ keyword)
21+
res.raise_for_status()
22+
soup = bs4.BeautifulSoup(res.text)
23+
linkElems = soup.select('.r a')
24+
numOpen = min(5, len(linkElems))
25+
26+
for i in range(numOpen):
27+
webbrowser.open('http://google.com' + linkElems[i].get('href'))
28+
29+
if __name__ == '__main__':
30+
main()

0 commit comments

Comments
 (0)