Python Glossary Русский

all Python 3webbrowserfunction

webbrowser.open()

opening pages

Opens a URL in the default browser; new=0 means the same window, 1 a new window, 2 a new tab. Returns True or False.

Syntax

webbrowser.open(url, new=0, autoraise=True)

Failure is not an exception but False: on a headless server there is no browser and the call simply does nothing. If only a text-mode browser (lynx, w3m) is found, it takes over the terminal and the program waits for it to close. The new values are only a hint. For a local file pass Path('page.html').resolve().as_uri(); a bare path does not work everywhere.

Examples

import webbrowser
class Recorder(webbrowser.BaseBrowser):  # поддельный браузер: ничего не открывает
    def open(self, url, new=0, autoraise=True):
        print('open', url, 'new =', new)
        return True
webbrowser.register('recorder', Recorder, preferred=True)
print(webbrowser.open('https://python.org'))  # → open https://python.org new = 0 / True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy