Python development of the _webbrowser_ browser control module
Recommended for you: Get network issues from WhatsUp Gold. Not end users.
''' WebBrowser Python module supports some operation on the browser There are three main methods: webbrowser.open(url, new=0, autoraise=True) webbrowser.open_new(url) webbrowser.open_new_tab(url) In the webbrowser.py file, we can see the source code: ######################################################## def open(url, new=0, autoraise=True): for name in _tryorder: browser = get(name) if browser.open(url, new, autoraise): return True return False def open_new(url): return open(url, 1) def open_new_tab(url): return open(url, 2) ######################################################## Can be seen behind the two method, is based on the first method (open method). So we need to understand the webbrowser.open () method: webbrowser.open(url, new=0, autoraise=True) Access to the URL address, in the default browser system if new=0, URL will be in the same Open a browser window; if new=1, a new browser window is opened;new=2 The new tab browser will open. While the webbrowser.get () method can access to the system operation object browser. webbrowser.register()Method can be registered browser type, and allows the type name is registered as follows: Type Name Class Name Notes 'mozilla' Mozilla('mozilla') 'firefox' Mozilla('mozilla') 'netscape' Mozilla('netscape') 'galeon' Galeon('galeon') 'epiphany' Galeon('epiphany') 'skipstone' BackgroundBrowser('skipstone') 'kfmclient' Konqueror() (1) 'konqueror' Konqueror() (1) 'kfm' Konqueror() (1) 'mosaic' BackgroundBrowser('mosaic') 'opera' Opera() 'grail' Grail() 'links' GenericBrowser('links') 'elinks' Elinks('elinks') 'lynx' GenericBrowser('lynx') 'w3m' GenericBrowser('w3m') 'windows-default' WindowsDefault (2) 'macosx' MacOSX('default') (3) 'safari' MacOSX('safari') (3) 'google-chrome' Chrome('google-chrome') 'chrome' Chrome('chrome') 'chromium' Chromium('chromium') 'chromium-browser' Chromium('chromium-browser') Notes: 1. “Konqueror” is the file manager for the KDE desktop environment for Unix, and only makes sense to use if KDE is running. Some way of reliably detecting KDE would be nice; the KDEDIR variable is not sufficient. Note also that the name “kfm” is used even when using the konqueror command with KDE 2 — the implementation selects the best strategy for running Konqueror. 2. Only on Windows platforms. 3. Only on Mac OS X platform. '''
Below is my demo, when demo is running, the system default browser will open: http://www.baidu.com/
=========================================
Part of the code:
=========================================
1 #python webbrowser 2 3 import webbrowser 4 5 ''' 6 WebBrowser Python module supports some operation on the browser 7 There are three main methods: 8 webbrowser.open(url, new=0, autoraise=True) 9 webbrowser.open_new(url) 10 webbrowser.open_new_tab(url) 11 12 In the webbrowser.py file, we can see the source code: 13 ######################################################## 14 def open(url, new=0, autoraise=True): 15 for name in _tryorder: 16 browser = get(name) 17 if browser.open(url, new, autoraise): 18 return True 19 return False 20 21 def open_new(url): 22 return open(url, 1) 23 24 def open_new_tab(url): 25 return open(url, 2) 26 ######################################################## 27 Can be seen behind the two method, is based on the first method (open method). 28 So we need to understand the webbrowser.open () method: 29 webbrowser.open(url, new=0, autoraise=True) 30 Access to the URL address, in the default browser system if new=0, URL will be in the same 31 Open a browser window; if new=1, a new browser window is opened;new=2 32 The new tab browser will open. 33 34 While the webbrowser.get () method can access to the system operation object browser. 35 webbrowser.register()Method can be registered browser type, and allows the type name is registered as follows: 36 Type Name Class Name Notes 37 'mozilla' Mozilla('mozilla') 38 'firefox' Mozilla('mozilla') 39 'netscape' Mozilla('netscape') 40 'galeon' Galeon('galeon') 41 'epiphany' Galeon('epiphany') 42 'skipstone' BackgroundBrowser('skipstone') 43 'kfmclient' Konqueror() (1) 44 'konqueror' Konqueror() (1) 45 'kfm' Konqueror() (1) 46 'mosaic' BackgroundBrowser('mosaic') 47 'opera' Opera() 48 'grail' Grail() 49 'links' GenericBrowser('links') 50 'elinks' Elinks('elinks') 51 'lynx' GenericBrowser('lynx') 52 'w3m' GenericBrowser('w3m') 53 'windows-default' WindowsDefault (2) 54 'macosx' MacOSX('default') (3) 55 'safari' MacOSX('safari') (3) 56 'google-chrome' Chrome('google-chrome') 57 'chrome' Chrome('chrome') 58 'chromium' Chromium('chromium') 59 'chromium-browser' Chromium('chromium-browser') 60 61 Notes: 62 1. “Konqueror” is the file manager for the KDE desktop environment for Unix, and only makes sense to use if KDE is running. Some way of reliably detecting KDE would be nice; the KDEDIR variable is not sufficient. Note also that the name “kfm” is used even when using the konqueror command with KDE 2 — the implementation selects the best strategy for running Konqueror. 63 2. Only on Windows platforms. 64 3. Only on Mac OS X platform. 65 66 67 ''' 68 69 __author__ = {'name' : 'Hongten', 70 'mail' : '', 71 'blog' : 'http://www.cnblogs.com/', 72 'QQ': '648719819', 73 'created' : '2013-09-20'} 74 75 #global var 76 URL = None 77 78 def ove_open(url): 79 '''webbrowser.open().''' 80 if url is not None and url != '': 81 return webbrowser.open(url) 82 else: 83 print('the URL is None or Empty!') 84 85 def ove_open_new(url): 86 '''webbrowser.open_new().''' 87 if url is not None and url != '': 88 return webbrowser.open_new(url) 89 else: 90 print('the URL is None or Empty!') 91 92 def ove_open_new_tab(url): 93 '''webbrowser.open_new_tab().''' 94 if url is not None and url != '': 95 return webbrowser.open_new_tab(url) 96 else: 97 print('the URL is None or Empty!') 98 99 def ove_get(): 100 return webbrowser.get() 101 102 def test_open(): 103 ove_open(URL) 104 105 def test_open_new(): 106 ove_open_new(URL) 107 108 def test_open_new_tab(): 109 ove_open_new_tab(URL) 110 111 def test_get(): 112 type_name = ove_get() 113 print(type_name) 114 115 def init(): 116 global URL 117 URL = 'http://www.baidu.com/' 118 119 def main(): 120 init() 121 test_open() 122 test_open_new() 123 test_open_new_tab() 124 test_get() 125 126 if __name__ == '__main__': 127 main()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
Posted by Jerry at November 27, 2013 - 3:14 AM