zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    视频高清直播RTMP视频推流组件EasyRTMP-IOS版如何使用wchar_t*类型参数?
    设计模式
    算法学习【第10篇】:算法之动态规划问题
    算法学习【第9篇】:算法之斐波那契数列
    算法学习【第8篇】:贪心算法找零问题
    算法学习【第7篇】:算法之迷宫问题
    算法学习【第6篇】:算法之数据结构
    算法学习【第5篇】:常用排序算法(*******)
    算法学习【第4篇】:算法之---堆的简单介绍
    算法学习【第3篇】:树和二叉树简介
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_python_webbrowser.html
Copyright © 2011-2022 走看看