Python Forum
Web scraping: webbrowser issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web scraping: webbrowser issue
#1
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...
Reply
#2
I don't know what you are doing, it works when I try it.
Reply
#3
"which" was added to "shutil" in python 3.3.

So somewhere you are really running python 3.2 or less, not python3.6
Recommended Tutorials:
Reply
#4
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.
Reply
#5
Bingo! After removing shutil.py it works fine.
Reply
#6
A lot of newbies hit that gotcha
https://python-forum.io/Thread-Basic-Python-Gotchas
Recommended Tutorials:
Reply
#7
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?
Reply
#8
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")
Recommended Tutorials:
Reply
#9
(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.
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Webbrowser.open() causes errors ChrisOfBristol 1 883 Apr-09-2023, 08:34 PM
Last Post: deanhystad
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,511 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  Scraping Issue with BS muzikman 11 3,105 Dec-10-2021, 08:49 AM
Last Post: muzikman
  Web scraping Possible JavaScript issue johnboy1974 2 2,072 Apr-11-2021, 08:53 AM
Last Post: johnboy1974
  How to get a URL from python 'webbrowser'? dheeraj 0 1,862 Apr-05-2021, 03:55 PM
Last Post: dheeraj
  Webbrowser jbrick97 3 60,265 Sep-27-2020, 03:58 AM
Last Post: ndc85430
  Making WebBrowser In PySide2 Harshil 0 1,904 Sep-16-2020, 05:03 PM
Last Post: Harshil
  webbrowser and menu not working. sik 1 1,839 Oct-31-2019, 03:39 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

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