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
#21
Yes, will open a new topic. Maybe somenone will know.
Reply
#22
I think you should open a different topic ;)
Reply
#23
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.
Reply
#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
#25
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)
Reply
#26
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?
Reply
#27
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
Reply
#28
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)
Reply
#29
(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?
Reply
#30
(Apr-30-2019, 04:50 AM)lukkizy Wrote: please with this code how will one be able to access the 5 google
links?
In your browser. It opens them in your default web browser.
Reply


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,272 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  "I'm Feeling Lucky" script problem (again) tab_lo_lo 7 7,822 Jul-23-2019, 11:26 PM
Last Post: snippsat
  How to use BeautifulSoup to parse google search results DevinGP 16 21,360 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