W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
You want to launch a browser from a script and have it point to some URL that youspecify.
The webbrowser module can be used to launch a browser in a platform-independentmanner. For example:
>>> import webbrowser
>>> webbrowser.open('http://www.python.org')
True
>>>
This opens the requested page using the default browser. If you want a bit more controlover how the page gets opened, you can use one of the following functions:
>>> # Open the page in a new browser window
>>> webbrowser.open_new('http://www.python.org')
True
>>>
>>> # Open the page in a new browser tab
>>> webbrowser.open_new_tab('http://www.python.org')
True
>>>
These will try to open the page in a new browser window or tab, if possible and supportedby the browser.If you want to open a page in a specific browser, you can use the webbrowser.get()function to specify a particular browser. For example:
>>> c = webbrowser.get('firefox')
>>> c.open('http://www.python.org')
True
>>> c.open_new_tab('http://docs.python.org')
True
>>>
A full list of supported browser names can be found in the Python documentation.
Being able to easily launch a browser can be a useful operation in many scripts. Forexample, maybe a script performs some kind of deployment to a server and you’d liketo have it quickly launch a browser so you can verify that it’s working. Or maybe aprogram writes data out in the form of HTML pages and you’d just like to fire up abrowser to see the result. Either way, the webbrowser module is a simple solution.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: