huhahapz 发表于 2015-4-26 09:12:19

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.iyunv.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()
  
页: [1]
查看完整版本: python开发_webbrowser_浏览器控制模块