python開發_webbrowser_瀏覽器控制模塊


'''
    python的webbrowser模塊支持對瀏覽器進行一些操作
    主要有以下三個方法:
        webbrowser.open(url, new=0, autoraise=True)
        webbrowser.open_new(url)
        webbrowser.open_new_tab(url)

    在webbrowser.py文件中,我們可以看到源碼:
    ########################################################
        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)
    ########################################################
    可以看出后面兩個方法,都是建立在第一個方法open()方法上面的。
    所以我們需要了解webbrowser.open()方法:
        webbrowser.open(url, new=0, autoraise=True)
            在系統的默認瀏覽器中訪問url地址,如果new=0,url會在同一個
            瀏覽器窗口中打開;如果new=1,新的瀏覽器窗口會被打開;new=2
            新的瀏覽器tab會被打開。

    而webbrowser.get()方法可以獲取到系統瀏覽器的操作對象。
    webbrowser.register()方法可以注冊瀏覽器類型,而允許被注冊的類型名稱如下:
    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. 


'''

下面是我做的demo,在demo運行的時候,系統默認的瀏覽器會打開:http://www.baidu.com/

=========================================

代碼部分:

=========================================

  1 #python webbrowser
  2 
  3 import webbrowser
  4 
  5 '''
  6     python的webbrowser模塊支持對瀏覽器進行一些操作
  7     主要有以下三個方法:
  8         webbrowser.open(url, new=0, autoraise=True)
  9         webbrowser.open_new(url)
 10         webbrowser.open_new_tab(url)
 11 
 12     在webbrowser.py文件中,我們可以看到源碼:
 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     可以看出后面兩個方法,都是建立在第一個方法open()方法上面的。
 28     所以我們需要了解webbrowser.open()方法:
 29         webbrowser.open(url, new=0, autoraise=True)
 30             在系統的默認瀏覽器中訪問url地址,如果new=0,url會在同一個
 31             瀏覽器窗口中打開;如果new=1,新的瀏覽器窗口會被打開;new=2
 32             新的瀏覽器tab會被打開。
 33 
 34     而webbrowser.get()方法可以獲取到系統瀏覽器的操作對象。
 35     webbrowser.register()方法可以注冊瀏覽器類型,而允許被注冊的類型名稱如下:
 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' : 'hongtenzone@foxmail.com',
 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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM