-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-search.py
68 lines (53 loc) · 2.37 KB
/
web-search.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Web Search
#Developed by : PythonCoder8
#Requires Pip installation : pip install google
#Description : Search web for query and retrieve a selected amount of URLs
#Only tested with Python version 3.8.5. May not work on Python version 3.8 or under
#########################################################################################
try:
from time import time
from googlesearch import search
import sys
import webbrowser
#Print title using variables and delete after using variables to clear up RAM
width = 40
titletxt = 'Web-Search'
title = titletxt.center(width)
print(title)
del width
del titletxt
del title
search_query = input('\nWhat do you want to search for on the web?: ')
result_num = input('How many results do you want to retrieve from the web?: ')
#Verify user input
try:
int_result_num = int(result_num)
except:
sys.exit('The number of results you wanted to retrieve was not a number. Exiting program...')
#Display web results
print('The top %d results from the web are:' %(int_result_num))
start = time()
for url in search(search_query, tld='com', stop=int_result_num):
print(url + " - %s" %(url.split('/')[2]))
end = time()
print('Found %d results from the web in %s' %(int_result_num, end - start) + ' seconds.')
#Delete time variables to clear RAM
del start
del end
#Ask user if they want to open the links in the web browser with validation
open_in_browser = input('Do you want to open the given URLs in your web browser (Y/N)? ')
if open_in_browser.upper() == 'Y':
for url in search(search_query, tld='com', stop=int_result_num):
webbrowser.open(url)
elif open_in_browser.upper() == 'N':
sys.exit('Ok! Bye!')
else:
while open_in_browser.upper() != 'Y' and open_in_browser.upper() != 'N':
open_in_browser = input('Invalid response! Do you want to open the given URLs in your web browser (Y/N)? ')
if open_in_browser.upper() == 'Y':
for url in search(search_query, tld='com', stop=int_result_num):
webbrowser.open(url)
elif open_in_browser.upper() == 'N':
sys.exit('Ok! Bye!')
except:
print("Something went wrong.")