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
#11
Error:
C:\Python36\kodovi>python -m lucky1.py superman C:\Python36\python.exe: Error while finding module specification for 'lucky1.py' (AttributeError: module 'lucky1' has no attribute '__path__')
this is what I get...
Reply
#12
sorry, remove the -m, my mistake
Reply
#13
Output:
C:\Python36\kodovi>python lucky.py superman Googling... None
still nothing, there must be some mistake in the logic of the code.
Reply
#14
I added a few things to your code so that i can get a better idea of what's happening.
I believe that the code is running ok, but the url is wrong. You can try it in a browser and nothing happens
#! python3
# lucky.py - Opens several Google search results.
  
import webbrowser
from bs4 import BeautifulSoup
import requests
import sys
 
def my_func(search_str) :
    print('Googling...') # display text while downloading the Google page
    url = 'http://google.com/search?source=' + ' '.join(search_str)
    print(f'url: {url}')
    res = requests.get(url)
    print(res.status_code)
    # print(res.raise_for_status())
  
    # Retreive top search result links.
    soup = 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(args):
    print(args[1:])
    my_func(args[1:])
 
if __name__ == '__main__':
    test_it(sys.argv)
output:
Output:
(venv) Larz60p@linux-nnem: forum:$/run/media/.../lucky.py superman ['superman'] Googling... url: http://google.com/search?source=superman 200 (venv) Larz60p@linux-nnem: forum:$
you can see that the status is 200 which is success.
I separated the url so that I could display it
It works, but wrong url

You need to check google's docs to get proper format.
Reply
#15
What is status code 200? Is it a number of results (urls)?

Some good news! Try out this code:

#! 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
    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)

Just to mention that my first code works too:

#! python3
# lucky.py - Opens several Google search results.

import webbrowser, bs4, requests, sys

print('Googling...') # display text while downloading the Google page
res = requests.get('http://google.com/search?q=' + ' '.join(sys.argv[1:]))
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')
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'))
Is there any reason why is it better todo things in your way, with defining functions?
Reply
#16
status code 200 is a network status code. It means success.
You can get a list of all internet status codes from rfc-editor.org
functions are reusable if written properly, inline code is not
When code starts getting large, you should break into modules, and import what you need.
You should also use a version control system, there are several good ones, I use git, and as a repository, github.
I also tend to use Classes for most of what I write.
Reply
#17
Thank you. I installed git and have github profile but don't know how to use it. I'll have to listen to tutorial just for that...and I don't have much time.
One more question from a noob :) Let's say that I wrote this program for a client who doesn't have installed Python on his machine. I send him .py file and then what? How can he use it?
Reply
#18
You need to bundle it as an exe file, which is done using pyinstaller: http://www.pyinstaller.org/
Something else to learn, but not difficult!
Reply
#19
Thank you.
the questions never end - is it a common thing to put a code in github repository in raw form or as exe file? Or maybe both?

I tried to bundle it and got this:
[Image: kb8x0g.png]

There is some error. And checking my folder with codes I have lucky1.spec which doesn't work.
Reply
#20
I use github when I'm building something that I want to share, or am working on with someone else, but not for everyday coding.
Can't help you much with pyinstaller. My experience is very limited.
You might want to start a new thread, just for your pyinstaller learning.
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,260 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  "I'm Feeling Lucky" script problem (again) tab_lo_lo 7 7,792 Jul-23-2019, 11:26 PM
Last Post: snippsat
  How to use BeautifulSoup to parse google search results DevinGP 16 21,335 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