前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python开发_webbrowser_浏览器控制模块

python开发_webbrowser_浏览器控制模块

作者头像
Hongten
发布2018-09-13 12:09:12
1.2K0
发布2018-09-13 12:09:12
举报
文章被收录于专栏:HongtenHongten
代码语言:javascript
复制
'''
    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/

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

代码部分:

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

代码语言:javascript
复制
  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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档