|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You want to launch a browser from a script and have it point to some URL that you |
9 |
| -specify. |
| 8 | +你想通过脚本启动浏览器并打开指定的URL网页 |
10 | 9 |
|
11 | 10 | |
|
12 | 11 |
|
13 | 12 | ----------
|
14 | 13 | 解决方案
|
15 | 14 | ----------
|
16 |
| -The webbrowser module can be used to launch a browser in a platform-independent |
17 |
| -manner. For example: |
| 15 | +``webbrowser`` 模块能被用来启动一个浏览器,并且与平台无关。例如: |
18 | 16 |
|
19 |
| ->>> import webbrowser |
20 |
| ->>> webbrowser.open('http://www.python.org') |
21 |
| -True |
22 |
| ->>> |
| 17 | +.. code-block:: python |
23 | 18 |
|
24 |
| -This opens the requested page using the default browser. If you want a bit more control |
25 |
| -over how the page gets opened, you can use one of the following functions: |
| 19 | + >>> import webbrowser |
| 20 | + >>> webbrowser.open('http://www.python.org') |
| 21 | + True |
| 22 | + >>> |
26 | 23 |
|
27 |
| ->>> # Open the page in a new browser window |
28 |
| ->>> webbrowser.open_new('http://www.python.org') |
29 |
| -True |
30 |
| ->>> |
| 24 | +它会使用默认浏览器打开指定网页。如果你还想对网页打开方式做更多控制,还可以使用下面这些函数: |
31 | 25 |
|
32 |
| ->>> # Open the page in a new browser tab |
33 |
| ->>> webbrowser.open_new_tab('http://www.python.org') |
34 |
| -True |
35 |
| ->>> |
| 26 | +.. code-block:: python |
36 | 27 |
|
37 |
| -These will try to open the page in a new browser window or tab, if possible and supported |
38 |
| -by the browser. |
39 |
| -If you want to open a page in a specific browser, you can use the webbrowser.get() |
40 |
| -function to specify a particular browser. For example: |
| 28 | + >>> # Open the page in a new browser window |
| 29 | + >>> webbrowser.open_new('http://www.python.org') |
| 30 | + True |
| 31 | + >>> |
41 | 32 |
|
42 |
| ->>> c = webbrowser.get('firefox') |
43 |
| ->>> c.open('http://www.python.org') |
44 |
| -True |
45 |
| ->>> c.open_new_tab('http://docs.python.org') |
46 |
| -True |
47 |
| ->>> |
| 33 | + >>> # Open the page in a new browser tab |
| 34 | + >>> webbrowser.open_new_tab('http://www.python.org') |
| 35 | + True |
| 36 | + >>> |
48 | 37 |
|
49 |
| -A full list of supported browser names can be found in the Python documentation. |
| 38 | +这样就可以打开一个新的浏览器窗口或者标签,只要浏览器支持就行。 |
| 39 | + |
| 40 | +如果你想指定浏览器类型,可以使用 ``webbrowser.get()`` 函数来指定某个特定浏览器。例如: |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + >>> c = webbrowser.get('firefox') |
| 45 | + >>> c.open('http://www.python.org') |
| 46 | + True |
| 47 | + >>> c.open_new_tab('http://docs.python.org') |
| 48 | + True |
| 49 | + >>> |
| 50 | +
|
| 51 | +对于支持的浏览器名称列表可查阅`Python文档 <http://docs.python.org/3/library/webbrowser.html>`_ |
50 | 52 |
|
51 | 53 | |
|
52 | 54 |
|
53 | 55 | ----------
|
54 | 56 | 讨论
|
55 | 57 | ----------
|
56 |
| -Being able to easily launch a browser can be a useful operation in many scripts. For |
57 |
| -example, maybe a script performs some kind of deployment to a server and you’d like |
58 |
| -to have it quickly launch a browser so you can verify that it’s working. Or maybe a |
59 |
| -program writes data out in the form of HTML pages and you’d just like to fire up a |
60 |
| -browser to see the result. Either way, the webbrowser module is a simple solution. |
| 58 | +在脚本中打开浏览器有时候会很有用。例如,某个脚本执行某个服务器发布任务, |
| 59 | +你想快速打开一个浏览器来确保它已经正常运行了。 |
| 60 | +或者是某个程序以HTML网页格式输出数据,你想打开浏览器查看结果。 |
| 61 | +不管是上面哪种情况,使用 ``webbrowser`` 模块都是一个简单实用的解决方案。 |
61 | 62 |
|
0 commit comments