Project: “I’m Feeling Lucky” Google Search - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Project: “I’m Feeling Lucky” Google Search (/thread-11674.html) |
RE: Project: “I’m Feeling Lucky” Google Search - Truman - Jul-27-2018 Yes, will open a new topic. Maybe somenone will know. RE: Project: “I’m Feeling Lucky” Google Search - Truman - Jul-27-2018 I think you should open a different topic ;) RE: Project: “I’m Feeling Lucky” Google Search - Larz60+ - Jul-28-2018 krish143, What you just did is called hijacking someone else's thread. You need to start your own. And before doing that read and understand the forum rules: https://python-forum.io/misc.php?action=help especially the part about BBCODE tags. RE: Project: “I’m Feeling Lucky” Google Search - Truman - Jul-28-2018 Larz, I was finally able to bundle lucky1.py file by removing os.py that was in the same folder from some previous coding. But the problem is that command prompt opens for a second ( I see three printed lines: Googling, url..., 200). Does this mean that program should be better designed for user? Maybe I should first ask a question with input? #! python3 # lucky1.py - Opens several Google search results. import webbrowser import bs4 import requests import sys client = input("Hi! Please add search term: ") def my_func(search_str): print('Googling...') # display text while downloading the Google page url = 'http://google.com/search?q=' + ' '.join(search_str) print(f'url: {url}') res = requests.get(url) print(res.status_code) # Retreive top search result links. soup = bs4.BeautifulSoup(res.text, "html.parser") # Open a browser tab for each result linkElems = soup.select('.r a') for link in linkElems: print(f'link: {link}') numOpen = min(5, len(linkElems)) for i in range(numOpen): webbrowser.open('http://google.com' + linkElems[i].get('href')) def test_it(args): print(args[1:]) my_func(args[1:]) if __name__ == '__main__': test_it(sys.argv)I added input statement but it still doesn't work after adding search term. I assume that input is taken to be sys.argv? Or I have to pass sys.argv to client variable instead of test function? Now it makes more sense to me, I just don't know how to do it. :) RE: Project: “I’m Feeling Lucky” Google Search - Larz60+ - Jul-29-2018 sys.argv will only be valid if called from the command line (or from within certain IDE's) if you import the module into another program, you will have to provide the value passed to my_func, like: import Lucky1 ... Lucky1.my_func(value) RE: Project: “I’m Feeling Lucky” Google Search - Truman - Jul-29-2018 Does it mean that I can't write program in one file? I'm trying to see way how to use input value in those functions that I wrote. And then to bundle only one file. By the way, if program has several files how do I bundle it, how do I create one exe file that works on client's computer? RE: Project: “I’m Feeling Lucky” Google Search - Larz60+ - Jul-29-2018 I think you should start with a basic python tutorial. You are asking too many questions that you should know the answer to. here's a good one: https://www.python-course.eu/python3_course.php RE: Project: “I’m Feeling Lucky” Google Search - Truman - Jul-29-2018 In the meantime I made it: #! python3 # lucky1.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 url = 'http://google.com/search?q=' + ' '.join(search_str) print(f'url: {url}') res = requests.get(url) print(res.status_code) # Retreive top search result links. soup = bs4.BeautifulSoup(res.text, "html.parser") # Open a browser tab for each result linkElems = soup.select('.r a') for link in linkElems: print(f'link: {link}') numOpen = min(5, len(linkElems)) for i in range(numOpen): webbrowser.open('http://google.com' + linkElems[i].get('href')) def test_it(args): print(args) my_func(args) if __name__ == '__main__': client = input("Hi! Please add search term: ") test_it(client) RE: Project: “I’m Feeling Lucky” Google Search - lukkizy - Apr-30-2019 (Jul-29-2018, 10:08 PM)Truman Wrote: In the meantime I made it:#! python3 # lucky1.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 url = 'http://google.com/search?q=' + ' '.join(search_str) print(f'url: {url}') res = requests.get(url) print(res.status_code) # Retreive top search result links. soup = bs4.BeautifulSoup(res.text, "html.parser") # Open a browser tab for each result linkElems = soup.select('.r a') for link in linkElems: print(f'link: {link}') numOpen = min(5, len(linkElems)) for i in range(numOpen): webbrowser.open('http://google.com' + linkElems[i].get('href')) def test_it(args): print(args) my_func(args) if __name__ == '__main__': client = input("Hi! Please add search term: ") test_it(client) please with this code how will one be able to access the 5 google links? RE: Project: “I’m Feeling Lucky” Google Search - nilamo - Apr-30-2019 (Apr-30-2019, 04:50 AM)lukkizy Wrote: please with this code how will one be able to access the 5 googleIn your browser. It opens them in your default web browser. |