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
#2
Quote:I'm also not sure how to start this program
you 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.
Reply


Messages In This Thread
RE: Project: “I’m Feeling Lucky” Google Search - by Larz60+ - Jul-21-2018, 01:06 AM

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