Jul-21-2018, 01:06 AM
Quote:I'm also not sure how to start this programyou can create a function, for example named test_it() which executes a test case, then
start that with (untested):
#! python3 # lucky.py - Opens several Google search results. import webbrowser import bs4 import requests import sys def my_func(search_str) : print('Googling...') # display text while downloading the Google page res = requests.get('http://google.com/search?source=' + ' '.join(search_str)) print(res.raise_for_status()) # Retreive top search result links. soup = bs4.BeautifulSoup(res.text, "html.parser") # Open a browser tab for each result linkElems = soup.select('.r a') numOpen = min(5, len(linkElems)) for i in range(numOpen): webbrowser.open('http://google.com' + linkElems[i].get('href')) def test_it(): my_func(sys.argv[1:]) if __name__ == '__main__': test_it()this code will only run if the program is started from within an IDE, or from the command line, but will not run if your main code is called from another program.