search.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #Web Search
  2. #Developed by : PythonCoder8
  3. #Requires Pip installation : pip install google
  4. #Description : Search web for query and retrieve a selected amount of URLs
  5. #Python version : Major = 3
  6. #Only tested with 3.8.5
  7. #########################################################################################
  8. from time import time
  9. from googlesearch import search
  10. import sys
  11. import webbrowser
  12. search_query = input('What do you want to search for on the web?: ')
  13. result_num = input('How many results do you want to retrieve from the web?: ')
  14. try:
  15. int_result_num = int(result_num)
  16. except:
  17. sys.exit('The number of results you wanted to retrieve was not a number. Exiting program...')
  18. print('The top %d results from the web are:' %(int_result_num))
  19. start = time()
  20. for url in search(search_query, tld='com', stop=int_result_num):
  21. print(url)
  22. end = time()
  23. print('Found %d results from the web in %s' %(int_result_num, end - start) + ' seconds.')
  24. open_in_browser = input('Do you want to open the given URLs in your web browser (Y/N)? ')
  25. if open_in_browser.upper() == 'Y':
  26. for url in search(search_query, tld='com', stop=int_result_num):
  27. webbrowser.open(url)
  28. elif open_in_browser.upper() == 'N':
  29. sys.exit('Ok! Bye!')
  30. else:
  31. while open_in_browser.upper() != 'Y' and open_in_browser.upper() != 'N':
  32. open_in_browser = input('Invalid response! Do you want to open the given URLs in your web browser (Y/N)? ')
  33. if open_in_browser.upper() == 'Y':
  34. for url in search(search_query, tld='com', stop=int_result_num):
  35. webbrowser.open(url)
  36. elif open_in_browser.upper() == 'N':
  37. sys.exit('Ok! Bye!')