Python Forum
Project: “I’m Feeling Lucky” Google Search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project: “I’m Feeling Lucky” Google Search
#24
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. :)
Reply


Messages In This Thread
RE: Project: “I’m Feeling Lucky” Google Search - by Truman - Jul-28-2018, 11:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  With Selenium create a google Search list in Incognito mode withe specific location, tsurubaso 3 3,398 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  "I'm Feeling Lucky" script problem (again) tab_lo_lo 7 7,977 Jul-23-2019, 11:26 PM
Last Post: snippsat
  How to use BeautifulSoup to parse google search results DevinGP 16 21,780 Dec-22-2017, 10:23 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020