Python Glossary Русский

all Python 3webbrowserfunction

webbrowser.register()

browser controllers

Registers a browser type under a name so that webbrowser.get(name) or open() can use it.

Syntax

webbrowser.register(name, constructor, instance=None, *, preferred=False)

Without preferred=True the new browser goes to the end of the list and open() picks it only if the others are unavailable; with preferred=True it comes first. constructor is called later, without arguments, when the browser is needed; if a ready instance is given, constructor is never called and may be None. The registration lives only in the current process.

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://example.com'))  # → open https://example.com new = 0 / True
print(type(webbrowser.get('recorder')).__name__)  # → Recorder

See also

Open in the full glossarydocs.python.orgReport an inaccuracy