Python Forum

Full Version: Web scraping: webbrowser issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I started a new chapter in Automate Boring Stuff with Python called Web Scraping ( and I'm planning to focus on this field so if you have any other useful link you're welcome to share ) but since the start I'm facing an obstacle.
When I run the first code with Python IDLE
import webbrowser
webbrowser.open('http://inventwithpython.com/')
it opens web page pretty fast but when I write a code in notepad++ and try to start a created file in command prompt I receive an error:

Error:
C:\Python36\kodovi>webscrap.py Warning, log file not found starting a new one [17, 34, 23, 6, 4, 30, 5] 0.1451710693150684 11716 days, 0:00:00 Traceback (most recent call last): File "C:\Python36\kodovi\webscrap.py", line 1, in <module> import webbrowser File "C:\Python36\lib\webbrowser.py", line 532, in <module> if shutil.which(browser): AttributeError: module 'shutil' has no attribute 'which'
Not sure what's going on here...
I don't know what you are doing, it works when I try it.
"which" was added to "shutil" in python 3.3.

So somewhere you are really running python 3.2 or less, not python3.6
Check if you have named a file shutil.py,if you have rename it.
How it shall look.
λ ptpython
>>> import shutil

>>> shutil.__file__
'c:\\python36\\lib\\shutil.py'

>>> shutil.which
<function which at 0x036F5E40>

>>> help(shutil.which)
Help on function which in module shutil:

which(cmd, mode=1, path=None)
    Given a command, mode, and a PATH string, return the path which
    conforms to the given mode on the PATH, or None if there is no such
    file.

    `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
    of os.environ.get("PATH"), or can be overridden with a custom search
    path.
Bingo! After removing shutil.py it works fine.
Thank you for the link, will take a closer look.

regarding webbrowser module it opens web sites with Internet Explorer browser. Any idea how to change that to Firefox or any other browser just not this obsolete one?
you can see available browsers with webbrowser._tryorder and the order it will try to open them in
>>> import webbrowser as wb
>>> wb._tryorder
['xdg-open', 'gvfs-open', 'x-www-browser', 'firefox', 'google-chrome']
>>> wb.get('firefox').open('https://www.google.com')
Im not too sure about windows but you may have to register it?
import webbrowser
ffpath = 'C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'
print(webbrowser._tryorder)
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(ffpath), 1)
print(webbrowser._tryorder)
ff = webbrowser.get('firefox')
ff.open("http://www.google.com")
(Jul-10-2018, 11:22 PM)metulburr Wrote: [ -> ]Im not too sure about windows but you may have to register it?
Think the simplest way on Windows is to change default browser in Windows 10.
>>> import webbrowser
>>> webbrowser._tryorder
['windows-default', 'C:\\Program Files (x86)\\Internet Explorer\\IEXPLORE.EXE']
If windows-default is set to Chrome then webbrowser open in Chrome.
(Jul-10-2018, 11:22 PM)metulburr Wrote: [ -> ]Im not too sure about windows but you may have to register it?
import webbrowser
ffpath = 'C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'
print(webbrowser._tryorder)
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(ffpath), 1)
print(webbrowser._tryorder)
ff = webbrowser.get('firefox')
ff.open("http://www.google.com")

this is what I get
Error:
['windows-default', 'C:\\Program Files (x86)\\Internet Explorer\\IEXPLORE.EXE'] Traceback (most recent call last): File "C:\Python36\kodovi\browsers.py", line 3, in <module> wb.register('firefox', None, wb.BackgroundBrowser(ffpath), 1) NameError: name 'ffpath' is not defined
I practise Python on old laptop that has Windows 7. What I did is that I made Firefox a default browser through itself - Tools > Options. It's a simplier option, but I wanted to know is it possible to change a browser through a code.
Pages: 1 2